Skip to content

Commit d144099

Browse files
eduardboschkoral--
authored andcommitted
fix: Do not check http method by default
1 parent f4d41c1 commit d144099

File tree

4 files changed

+28
-6
lines changed

4 files changed

+28
-6
lines changed

dispatcher/src/main/kotlin/pl/droidsonroids/testing/mockwebserver/condition/HTTPMethod.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package pl.droidsonroids.testing.mockwebserver.condition
22

33
enum class HTTPMethod {
4+
ANY,
45
GET,
56
HEAD,
67
POST,

dispatcher/src/main/kotlin/pl/droidsonroids/testing/mockwebserver/condition/HttpUrlCondition.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ import okhttp3.mockwebserver.RecordedRequest
99
* Requests without <code>requestUrl</code>s are excluded from matching.
1010
*/
1111
abstract class HttpUrlCondition : Condition {
12-
internal open val httpMethod: HTTPMethod = HTTPMethod.GET
12+
internal open val httpMethod: HTTPMethod = HTTPMethod.ANY
1313

1414
override fun isRequestMatching(request: RecordedRequest): Boolean {
15-
if (request.method != httpMethod.name) {
15+
if (httpMethod != HTTPMethod.ANY && request.method != httpMethod.name) {
1616
return false
1717
}
1818

dispatcher/src/main/kotlin/pl/droidsonroids/testing/mockwebserver/condition/PathQueryConditionFactory.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class PathQueryConditionFactory constructor(private val pathPrefix: String = "")
2121
pathSuffix: String,
2222
queryParameterName: String,
2323
queryParameterValue: String? = null,
24-
httpMethod: HTTPMethod = HTTPMethod.GET,
24+
httpMethod: HTTPMethod = HTTPMethod.ANY,
2525
) =
2626
PathQueryCondition(
2727
pathPrefix + pathSuffix,
@@ -38,7 +38,7 @@ class PathQueryConditionFactory constructor(private val pathPrefix: String = "")
3838
*/
3939
fun withPathSuffix(
4040
pathSuffix: String,
41-
httpMethod: HTTPMethod = HTTPMethod.GET,
41+
httpMethod: HTTPMethod = HTTPMethod.ANY,
4242
) =
4343
PathQueryCondition(
4444
pathPrefix + pathSuffix,
@@ -52,7 +52,7 @@ class PathQueryConditionFactory constructor(private val pathPrefix: String = "")
5252
*/
5353
fun withPath(
5454
path: String,
55-
httpMethod: HTTPMethod = HTTPMethod.GET,
55+
httpMethod: HTTPMethod = HTTPMethod.ANY,
5656
) =
5757
PathQueryCondition(
5858
path,
@@ -76,7 +76,7 @@ class PathQueryConditionFactory constructor(private val pathPrefix: String = "")
7676
pathInfix: String,
7777
queryParameterName: String,
7878
queryParameterValue: String,
79-
httpMethod: HTTPMethod = HTTPMethod.GET,
79+
httpMethod: HTTPMethod = HTTPMethod.ANY,
8080
) =
8181
withPathSuffixAndQueryParameter(
8282
pathInfix,

dispatcher/src/test/kotlin/pl/droidsonroids/testing/mockwebserver/condition/HttpUrlConditionTest.kt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,25 @@ class HttpUrlConditionTest {
6464

6565
assertThat(matchAllUrlCondition.isRequestMatching(request)).isTrue
6666
}
67+
68+
@Test
69+
fun `request with URL with any request method should match any request method`() {
70+
HTTPMethod.values()
71+
.filter { it != HTTPMethod.ANY }
72+
.forEach { httpMethod->
73+
val matchAllUrlCondition = object : HttpUrlCondition() {
74+
override val httpMethod: HTTPMethod
75+
get() = HTTPMethod.ANY
76+
77+
override fun isUrlMatching(url: HttpUrl) = true
78+
override fun compareTo(other: Condition) = 0
79+
}
80+
val request =
81+
RecordedRequest("${httpMethod.name} /some/path HTTP/1.1", Headers.headersOf(), emptyList(), 0, Buffer(), 0, socket)
82+
83+
assertThat(matchAllUrlCondition.isRequestMatching(request))
84+
.withFailMessage { "${httpMethod.name} does not match condition" }
85+
.isTrue
86+
}
87+
}
6788
}

0 commit comments

Comments
 (0)