Skip to content
This repository was archived by the owner on Jun 11, 2024. It is now read-only.

Commit d97b3e1

Browse files
committed
Tests refactored
1 parent bf5666b commit d97b3e1

File tree

1 file changed

+36
-32
lines changed

1 file changed

+36
-32
lines changed

browserup-proxy-core/src/test/groovy/com/browserup/bup/mitmproxy/UnbalancedHarEntriesTest.groovy

Lines changed: 36 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -118,21 +118,23 @@ class UnbalancedHarEntriesTest extends MockServerTest {
118118
@Test
119119
void testMultipleRequestsAndForSlowOneWeGetOnlyResponseOnlyEntry() {
120120
//GIVEN
121-
def stubUrl = "/testResponseTimeoutCapturedInHar"
121+
def slowEndpointUrl = "/testSlowEndpoint"
122+
def fastEndpointUrl = "/testFastEndpoint.*"
122123
def targetServerDelaySec = 5
123124
def targetServiceResponseCode = 200
124125
def idleConnectionTimeoutSec = targetServerDelaySec + 1
125126

126-
configureMockServer(stubUrl, targetServerDelaySec, targetServiceResponseCode)
127+
configureMockServer(slowEndpointUrl, targetServerDelaySec, targetServiceResponseCode)
128+
configureMockServer(fastEndpointUrl, 0, targetServiceResponseCode)
127129

128130
proxy = startProxyAndCreateHar(idleConnectionTimeoutSec)
129131

130-
def requestUrl = "http://localhost:${mockServerPort}$stubUrl".toString()
132+
def requestUrl = "http://localhost:${mockServerPort}$slowEndpointUrl".toString()
131133

132134
def otherRequests = [
133-
'https://browserup.com/wp-content/themes/browserup/images/logo-text-475x93.png?1',
134-
'https://browserup.com/wp-content/themes/browserup/images/logo-text-475x93.png?2',
135-
'https://browserup.com/wp-content/themes/browserup/images/logo-text-475x93.png?3'
135+
"http://localhost:${mockServerPort}$fastEndpointUrl/?1".toString(),
136+
"http://localhost:${mockServerPort}$fastEndpointUrl/?2".toString(),
137+
"http://localhost:${mockServerPort}$fastEndpointUrl/?3".toString()
136138
]
137139

138140
//WHEN
@@ -143,7 +145,7 @@ class UnbalancedHarEntriesTest extends MockServerTest {
143145
}
144146
def totalNumberOfRequests = otherRequests.size() + 1
145147

146-
// Wait for 'other' requests
148+
// Wait for 'fast' requests
147149
await().atMost(7, TimeUnit.SECONDS).until {
148150
!responsesReceived.any { !it.get()}
149151
}
@@ -153,10 +155,10 @@ class UnbalancedHarEntriesTest extends MockServerTest {
153155
def har = proxy.getHar()
154156
assertEquals("Expected to get correct number of entries", totalNumberOfRequests, har.log.entries.size())
155157

156-
def harEntryForMockedServer = har.log.entries.find { it.request.url.contains("localhost") }
157-
def capturedUrl = harEntryForMockedServer.request.url
158+
def entryForSlowEndpoint = har.log.entries.find { it.request.url.contains(slowEndpointUrl) }
159+
def capturedUrl = entryForSlowEndpoint.request.url
158160
assertEquals("URL captured in HAR did not match request URL", requestUrl, capturedUrl)
159-
assertEquals("Expected response to be default", DEFAULT_HAR_RESPONSE, harEntryForMockedServer.response)
161+
assertEquals("Expected response to be default", DEFAULT_HAR_RESPONSE, entryForSlowEndpoint.response)
160162

161163
// Wait until response it received
162164
await().atMost(targetServerDelaySec + 1, TimeUnit.SECONDS).until({ responseReceived.get() })
@@ -165,34 +167,36 @@ class UnbalancedHarEntriesTest extends MockServerTest {
165167
har = proxy.getHar()
166168
assertEquals("Expected to get correct number of entries", totalNumberOfRequests, har.log.entries.size())
167169

168-
harEntryForMockedServer = har.log.entries.find { it.request.url.contains("localhost") }
169-
assertNotEquals("Expected request to be not default", DEFAULT_HAR_REQUEST, harEntryForMockedServer.request)
170-
assertEquals("URL captured in HAR did not match request URL", requestUrl, harEntryForMockedServer.request.url)
171-
assertNotEquals("Expected response to be not defualt", DEFAULT_HAR_RESPONSE, harEntryForMockedServer.response)
170+
entryForSlowEndpoint = har.log.entries.find { it.request.url.contains(slowEndpointUrl) }
171+
assertNotEquals("Expected request to be not default", DEFAULT_HAR_REQUEST, entryForSlowEndpoint.request)
172+
assertEquals("URL captured in HAR did not match request URL", requestUrl, entryForSlowEndpoint.request.url)
173+
assertNotEquals("Expected response to be not defualt", DEFAULT_HAR_RESPONSE, entryForSlowEndpoint.response)
172174
assertEquals("Got unexpected response status",
173-
harEntryForMockedServer.response.status, targetServiceResponseCode)
175+
entryForSlowEndpoint.response.status, targetServiceResponseCode)
174176
assertNotEquals("Expected response http version to be populated",
175-
harEntryForMockedServer.response.httpVersion, DEFAULT_HAR_RESPONSE.httpVersion)
177+
entryForSlowEndpoint.response.httpVersion, DEFAULT_HAR_RESPONSE.httpVersion)
176178
}
177179

178180
@Test
179181
void testMultipleRequestsAndAfterCleanHarWeGetOnlyOneResponseOnlyEntry() {
180182
//GIVEN
181-
def stubUrl = "/testResponseTimeoutCapturedInHar"
183+
def slowEndpointUrl = "/testSlowEndpoint"
184+
def fastEndpointUrl = "/testFastEndpoint.*"
182185
def targetServerDelaySec = 5
183186
def targetServiceResponseCode = 200
184187
def idleConnectionTimeoutSec = targetServerDelaySec + 1
185188

186-
configureMockServer(stubUrl, targetServerDelaySec, targetServiceResponseCode)
189+
configureMockServer(slowEndpointUrl, targetServerDelaySec, targetServiceResponseCode)
190+
configureMockServer(fastEndpointUrl, 0, targetServiceResponseCode)
187191

188192
proxy = startProxyAndCreateHar(idleConnectionTimeoutSec)
189193

190-
def requestUrl = "http://localhost:${mockServerPort}$stubUrl".toString()
194+
def requestUrl = "http://localhost:${mockServerPort}$slowEndpointUrl".toString()
191195

192196
def otherRequests = [
193-
'https://browserup.com/wp-content/themes/browserup/images/logo-text-475x93.png?1',
194-
'https://browserup.com/wp-content/themes/browserup/images/logo-text-475x93.png?2',
195-
'https://browserup.com/wp-content/themes/browserup/images/logo-text-475x93.png?3'
197+
"http://localhost:${mockServerPort}$fastEndpointUrl/?1".toString(),
198+
"http://localhost:${mockServerPort}$fastEndpointUrl/?2".toString(),
199+
"http://localhost:${mockServerPort}$fastEndpointUrl/?3".toString()
196200
]
197201

198202
//WHEN
@@ -203,7 +207,7 @@ class UnbalancedHarEntriesTest extends MockServerTest {
203207
}
204208
def totalNumberOfRequests = otherRequests.size() + 1
205209

206-
// Wait for 'other' requests
210+
// Wait for 'fast' requests
207211
await().atMost(7, TimeUnit.SECONDS).until {
208212
!responsesReceived.any { !it.get()}
209213
}
@@ -213,10 +217,10 @@ class UnbalancedHarEntriesTest extends MockServerTest {
213217
def har = proxy.getHar(true)
214218
assertEquals("Expected to get correct number of entries", totalNumberOfRequests, har.log.entries.size())
215219

216-
def harEntryForMockedServer = har.log.entries.find { it.request.url.contains("localhost") }
217-
def capturedUrl = harEntryForMockedServer.request.url
220+
def entryForSlowEndpoint = har.log.entries.find { it.request.url.contains(slowEndpointUrl) }
221+
def capturedUrl = entryForSlowEndpoint.request.url
218222
assertEquals("URL captured in HAR did not match request URL", requestUrl, capturedUrl)
219-
assertEquals("Expected response to be default", DEFAULT_HAR_RESPONSE, harEntryForMockedServer.response)
223+
assertEquals("Expected response to be default", DEFAULT_HAR_RESPONSE, entryForSlowEndpoint.response)
220224

221225
// Wait until response it received
222226
await().atMost(targetServerDelaySec + 1, TimeUnit.SECONDS).until({ responseReceived.get() })
@@ -225,13 +229,13 @@ class UnbalancedHarEntriesTest extends MockServerTest {
225229
har = proxy.getHar()
226230
assertEquals("Expected to get only one entry for slow request after clean har", 1, har.log.entries.size())
227231

228-
harEntryForMockedServer = har.log.entries.first()
229-
assertEquals("Expected request to be default", DEFAULT_HAR_REQUEST, harEntryForMockedServer.request)
230-
assertNotEquals("Expected response to be not defualt", DEFAULT_HAR_RESPONSE, harEntryForMockedServer.response)
232+
entryForSlowEndpoint = har.log.entries.first()
233+
assertEquals("Expected request to be default", DEFAULT_HAR_REQUEST, entryForSlowEndpoint.request)
234+
assertNotEquals("Expected response to be not defualt", DEFAULT_HAR_RESPONSE, entryForSlowEndpoint.response)
231235
assertEquals("Got unexpected response status",
232-
harEntryForMockedServer.response.status, targetServiceResponseCode)
236+
entryForSlowEndpoint.response.status, targetServiceResponseCode)
233237
assertNotEquals("Expected response http version to be populated",
234-
harEntryForMockedServer.response.httpVersion, DEFAULT_HAR_RESPONSE.httpVersion)
238+
entryForSlowEndpoint.response.httpVersion, DEFAULT_HAR_RESPONSE.httpVersion)
235239
}
236240

237241
@Test
@@ -282,7 +286,7 @@ class UnbalancedHarEntriesTest extends MockServerTest {
282286
}
283287

284288
private void configureMockServer(String url, int delaySec, int responseCode) {
285-
stubFor(get(urlEqualTo(url))
289+
stubFor(get(urlMatching(url))
286290
.willReturn(aResponse().withStatus(responseCode)
287291
.withFixedDelay(TimeUnit.SECONDS.toMillis(delaySec) as Integer)
288292
.withBody("success"))

0 commit comments

Comments
 (0)