12
12
import java .util .List ;
13
13
import java .util .Map ;
14
14
import java .util .Set ;
15
+ import nl .basjes .parse .useragent .UserAgent ;
15
16
import org .testng .Assert ;
16
17
import org .testng .annotations .BeforeMethod ;
17
18
import org .testng .annotations .Test ;
@@ -61,6 +62,7 @@ public void testExemptions() {
61
62
62
63
for (ThrottleEvent event : ALL_EVENTS ) {
63
64
when (mockRequestContext .getUserAgent ()).thenReturn (null );
65
+ when (mockRequestContext .getAgentClass ()).thenReturn (null );
64
66
try {
65
67
APIThrottle .evaluate (opContext , Set .of (event ), false );
66
68
} catch (Exception ex ) {
@@ -76,12 +78,16 @@ public void testExemptions() {
76
78
for (String ua : exemptions ) {
77
79
try {
78
80
when (mockRequestContext .getUserAgent ()).thenReturn (ua );
81
+ when (mockRequestContext .getAgentClass ())
82
+ .thenReturn (RequestContext .UAA .parse (ua ).get (UserAgent .AGENT_CLASS ).getValue ());
79
83
APIThrottle .evaluate (opContext , Set .of (event ), true );
80
84
} catch (Exception ex ) {
81
85
Assert .fail ("Exception was thrown and NOT expected! " + event );
82
86
}
83
87
try {
84
88
when (mockRequestContext .getUserAgent ()).thenReturn (ua );
89
+ when (mockRequestContext .getAgentClass ())
90
+ .thenReturn (RequestContext .UAA .parse (ua ).get (UserAgent .AGENT_CLASS ).getValue ());
85
91
APIThrottle .evaluate (opContext , Set .of (event ), false );
86
92
} catch (Exception ex ) {
87
93
Assert .fail ("Exception was thrown and NOT expected! " + event );
@@ -106,6 +112,8 @@ public void testThrottleException() {
106
112
&& !event .getActiveThrottles ().contains (MANUAL )) {
107
113
try {
108
114
when (mockRequestContext .getUserAgent ()).thenReturn (ua );
115
+ when (mockRequestContext .getAgentClass ())
116
+ .thenReturn (RequestContext .UAA .parse (ua ).get (UserAgent .AGENT_CLASS ).getValue ());
109
117
APIThrottle .evaluate (opContext , Set .of (event ), true );
110
118
Assert .fail (String .format ("Exception WAS expected! %s %s" , ua , event ));
111
119
} catch (Exception ignored ) {
@@ -115,6 +123,8 @@ public void testThrottleException() {
115
123
&& !event .getActiveThrottles ().contains (MANUAL )) {
116
124
try {
117
125
when (mockRequestContext .getUserAgent ()).thenReturn (ua );
126
+ when (mockRequestContext .getAgentClass ())
127
+ .thenReturn (RequestContext .UAA .parse (ua ).get (UserAgent .AGENT_CLASS ).getValue ());
118
128
APIThrottle .evaluate (opContext , Set .of (event ), true );
119
129
} catch (Exception ex ) {
120
130
Assert .fail (String .format ("Exception was thrown and NOT expected! %s %s" , ua , event ));
@@ -126,6 +136,8 @@ public void testThrottleException() {
126
136
&& !event .getActiveThrottles ().contains (MANUAL )) {
127
137
try {
128
138
when (mockRequestContext .getUserAgent ()).thenReturn (ua );
139
+ when (mockRequestContext .getAgentClass ())
140
+ .thenReturn (RequestContext .UAA .parse (ua ).get (UserAgent .AGENT_CLASS ).getValue ());
129
141
APIThrottle .evaluate (opContext , Set .of (event ), false );
130
142
Assert .fail (String .format ("Exception WAS expected! %s %s" , ua , event ));
131
143
} catch (Exception ignored ) {
@@ -135,6 +147,8 @@ public void testThrottleException() {
135
147
&& !event .getActiveThrottles ().contains (MANUAL )) {
136
148
try {
137
149
when (mockRequestContext .getUserAgent ()).thenReturn (ua );
150
+ when (mockRequestContext .getAgentClass ())
151
+ .thenReturn (RequestContext .UAA .parse (ua ).get (UserAgent .AGENT_CLASS ).getValue ());
138
152
APIThrottle .evaluate (opContext , Set .of (event ), false );
139
153
} catch (Exception ex ) {
140
154
Assert .fail (String .format ("Exception was thrown and NOT expected! %s %s" , ua , event ));
@@ -145,12 +159,16 @@ public void testThrottleException() {
145
159
if (event .getActiveThrottles ().contains (MANUAL )) {
146
160
try {
147
161
when (mockRequestContext .getUserAgent ()).thenReturn (ua );
162
+ when (mockRequestContext .getAgentClass ())
163
+ .thenReturn (RequestContext .UAA .parse (ua ).get (UserAgent .AGENT_CLASS ).getValue ());
148
164
APIThrottle .evaluate (opContext , Set .of (event ), true );
149
165
Assert .fail (String .format ("Exception WAS expected! %s %s" , ua , event ));
150
166
} catch (Exception ignored ) {
151
167
}
152
168
try {
153
169
when (mockRequestContext .getUserAgent ()).thenReturn (ua );
170
+ when (mockRequestContext .getAgentClass ())
171
+ .thenReturn (RequestContext .UAA .parse (ua ).get (UserAgent .AGENT_CLASS ).getValue ());
154
172
APIThrottle .evaluate (opContext , Set .of (event ), false );
155
173
Assert .fail (String .format ("Exception WAS expected! %s %s" , ua , event ));
156
174
} catch (Exception ignored ) {
0 commit comments