Skip to content

Commit

Permalink
Merge branch 'main' into content_type_fix
Browse files Browse the repository at this point in the history
  • Loading branch information
joelrosario committed Jan 10, 2024
2 parents 33ee0f7 + e821718 commit 67cc48b
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ data class BooleanPattern(val example: String? = null) : Pattern, ScalarType {
return listOf(NullPattern)
}

override fun parse(value: String, resolver: Resolver): Value = when (value) {
override fun parse(value: String, resolver: Resolver): Value = when (value.lowercase()) {
!in listOf("true", "false") -> throw ContractException(mismatchResult(BooleanPattern(), value, resolver.mismatchMessages).toFailureReport())
else -> BooleanValue(value.toBoolean())
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,10 @@ internal class BooleanPatternTest {
"null"
)
}

@Test
fun `should parse a value with capital T or F as boolean` () {
assertThat(BooleanPattern().parse("True", Resolver())).isEqualTo(BooleanValue(true))
assertThat(BooleanPattern().parse("False", Resolver())).isEqualTo(BooleanValue(false))
}
}
73 changes: 70 additions & 3 deletions core/src/test/kotlin/in/specmatic/stub/HttpStubTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -996,7 +996,8 @@ paths:
fun `should return descriptive error message when there are no valid specifications loaded`() {
val httpStub = HttpStub(emptyList())
httpStub.use { stub ->
val response = stub.client.execute(HttpRequest("POST", "/data", emptyMap(), parsedJSONObject("""{"id": 10}""")))
val response =
stub.client.execute(HttpRequest("POST", "/data", emptyMap(), parsedJSONObject("""{"id": 10}""")))
assertThat(response.status).isEqualTo(400)
assertThat(response.body).isEqualTo(StringValue("No valid API specifications loaded"))
}
Expand All @@ -1007,7 +1008,8 @@ paths:
fun `should stub out a path having a space and return a randomised response`() {
val pathWithSpace = "/da ta"

val specification = OpenApiSpecification.fromFile("src/test/resources/openapi/spec_with_space_in_path.yaml").toFeature()
val specification =
OpenApiSpecification.fromFile("src/test/resources/openapi/spec_with_space_in_path.yaml").toFeature()

HttpStub(specification).use { stub ->
val request = HttpRequest("GET", pathWithSpace)
Expand Down Expand Up @@ -1091,7 +1093,8 @@ paths:
properties:
id:
type: integer
""".trimIndent(), "").toFeature()
""".trimIndent(), ""
).toFeature()

HttpStub(specification).use { stub ->
val request = HttpRequest("GET", "/data", queryParams = mapOf(queryParamWithSpace to "5"))
Expand All @@ -1108,6 +1111,70 @@ paths:
}
}

@Test
fun `should stub out a request for boolean query param with capital T or F in the incoming request`() {
val specification = createStubFromContracts(listOf("src/test/resources/openapi/spec_with_boolean_query.yaml"))

specification.use { stub ->
val request = HttpRequest("GET", "/data", queryParams = mapOf("enabled" to "True"))

val response = stub.client.execute(request)

assertThat(response.status).isEqualTo(200)
response.body.let {
assertThat(it).isInstanceOf(JSONObjectValue::class.java)
it as JSONObjectValue

assertThat(it.jsonObject["id"]).isInstanceOf(NumberValue::class.java)
}
}
}

@Test
fun `should recognize a request for boolean query param with capital T or F in the incoming request`() {
val specification = OpenApiSpecification.fromYAML(
"""
openapi: 3.0.1
info:
title: Random
version: "1"
paths:
/data:
get:
summary: Random
parameters:
- name: enabled
in: query
schema:
type: boolean
responses:
"200":
description: Random
content:
application/json:
schema:
type: object
properties:
id:
type: integer
""".trimIndent(), ""
).toFeature()

HttpStub(specification).use { stub ->
val request = HttpRequest("GET", "/data", queryParams = mapOf("enabled" to "True"))

val response = stub.client.execute(request)

assertThat(response.status).isEqualTo(200)
response.body.let {
assertThat(it).isInstanceOf(JSONObjectValue::class.java)
it as JSONObjectValue

assertThat(it.jsonObject["id"]).isInstanceOf(NumberValue::class.java)
}
}
}

@Test
fun `stub out a spec with no request body and respond to a request which has no body`() {
val specification = OpenApiSpecification.fromYAML("""
Expand Down
23 changes: 23 additions & 0 deletions core/src/test/resources/openapi/spec_with_boolean_query.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
openapi: 3.0.1
info:
title: Random
version: "1"
paths:
/data:
get:
summary: Random
parameters:
- name: enabled
in: query
schema:
type: boolean
responses:
"200":
description: Random
content:
application/json:
schema:
type: object
properties:
id:
type: integer
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"http-request": {
"method": "GET",
"path": "/data",
"query": {
"enabled": "True"
}
},
"http-response": {
"status": 200,
"body": {
"id": 10
}
}
}
2 changes: 1 addition & 1 deletion version.properties
Original file line number Diff line number Diff line change
@@ -1 +1 @@
version=1.2.4
version=1.2.6

0 comments on commit 67cc48b

Please sign in to comment.