Skip to content

Commit

Permalink
Add a condition to simulate a response timeout
Browse files Browse the repository at this point in the history
- Update README
  • Loading branch information
dmanluc authored and koral-- committed Jan 10, 2023
1 parent d420461 commit 3748ca3
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 2 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,13 @@ statusCode : 200
connectionFailure: true
```

Alternatively, you can specify not getting a response by simulating a timeout with `timeoutFailure` to `true`:

```yaml
statusCode : 200
timeoutFailure: true
```

Code without MockWebServer path dispatcher:

```kotlin
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ internal class Fixture {
internal set
var connectionFailure: Boolean = false
internal set
var timeoutFailure: Boolean = false
internal set

internal fun hasJsonBody() = body?.isPossibleJson() ?: false
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,13 @@ internal class MockResponseBuilder constructor(private val parser: ResourcesPars
mockResponse.addHeader(it)
}

if (fixture.connectionFailure) {
mockResponse.socketPolicy = SocketPolicy.DISCONNECT_AT_START
when {
fixture.connectionFailure -> {
mockResponse.socketPolicy = SocketPolicy.DISCONNECT_AT_START
}
fixture.timeoutFailure -> {
mockResponse.socketPolicy = SocketPolicy.NO_RESPONSE
}
}

return mockResponse
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,14 @@ internal class MockResponseBuilderTest {
assertThat(mockResponse.getBody()).isNull()
assertThat(mockResponse.socketPolicy).isEqualTo(SocketPolicy.DISCONNECT_AT_START)
}

@Test
fun `NO_RESPONSE set when timeout failure is true`() {
fixture.statusCode = 200
fixture.timeoutFailure = true
val mockResponse = builder.buildMockResponse("")
assertThat(mockResponse.status).contains("200")
assertThat(mockResponse.getBody()).isNull()
assertThat(mockResponse.socketPolicy).isEqualTo(SocketPolicy.NO_RESPONSE)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class YamlResourcesParserTest {
assertThat(fixture.headers).containsOnly("Content-Type: application/json")
assertThat(fixture.body).isEqualToIgnoringWhitespace("""{"test": null}""")
assertThat(fixture.connectionFailure).isFalse
assertThat(fixture.timeoutFailure).isFalse
}

@Test
Expand All @@ -39,6 +40,7 @@ class YamlResourcesParserTest {
assertThat(fixture.headers).containsOnly("Content-Type: application/json")
assertThat(fixture.body).isEqualToIgnoringWhitespace("""{"test": "\uD83D\uDC31"}""")
assertThat(fixture.connectionFailure).isFalse
assertThat(fixture.timeoutFailure).isFalse
}

@Test
Expand All @@ -48,6 +50,7 @@ class YamlResourcesParserTest {
assertThat(fixture.headers).isEmpty()
assertThat(fixture.body).isEqualToIgnoringWhitespace("[]")
assertThat(fixture.connectionFailure).isFalse
assertThat(fixture.timeoutFailure).isFalse
}

@Test
Expand All @@ -60,6 +63,7 @@ class YamlResourcesParserTest {
)
assertThat(fixture.body).isEqualTo("""{"test"}""")
assertThat(fixture.connectionFailure).isFalse
assertThat(fixture.timeoutFailure).isFalse
}

@Test
Expand All @@ -72,6 +76,7 @@ class YamlResourcesParserTest {
)
assertThat(fixture.body).isNull()
assertThat(fixture.connectionFailure).isFalse
assertThat(fixture.timeoutFailure).isFalse
}

@Test
Expand All @@ -83,6 +88,19 @@ class YamlResourcesParserTest {
)
assertThat(fixture.body).isNull()
assertThat(fixture.connectionFailure).isTrue
assertThat(fixture.timeoutFailure).isFalse
}

@Test
fun `parses response with timeout failure`() {
val fixture = parser.parseFrom("timeout_failure")
assertThat(fixture.statusCode).isEqualTo(200)
assertThat(fixture.headers).containsExactlyInAnyOrder(
"Content-Type: text/plain",
)
assertThat(fixture.body).isNull()
assertThat(fixture.connectionFailure).isFalse
assertThat(fixture.timeoutFailure).isTrue
}

@Test
Expand Down
4 changes: 4 additions & 0 deletions dispatcher/src/test/resources/fixtures/timeout_failure.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
statusCode: 200
headers:
- 'Content-Type: text/plain'
timeoutFailure: true

0 comments on commit 3748ca3

Please sign in to comment.