Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge the post request and response bodies while saving it as seed data in VS #1485

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import io.specmatic.core.value.JSONArrayValue
import io.specmatic.core.value.JSONObjectValue
import io.specmatic.core.value.StringValue
import io.specmatic.core.value.Value
import io.specmatic.core.value.mergeWith
import io.specmatic.mock.ScenarioStub
import io.specmatic.stub.ContractAndRequestsMismatch
import io.specmatic.stub.ContractStub
Expand Down Expand Up @@ -595,12 +596,23 @@ class StatefulHttpStub(

if (responseBody !is JSONObjectValue) return@forEach
if(httpRequest.method == "POST" && httpRequest.body !is JSONObjectValue) return@forEach
stubCache.addResponse(
path = resourcePath,
responseBody = responseBody,
idKey = DEFAULT_CACHE_RESPONSE_ID_KEY,
idValue = idValueFor(DEFAULT_CACHE_RESPONSE_ID_KEY, responseBody)
)

val requestBody = httpRequest.body
if(requestBody is JSONObjectValue) {
stubCache.addResponse(
path = resourcePath,
responseBody = requestBody.mergeWith(responseBody) as JSONObjectValue,
idKey = DEFAULT_CACHE_RESPONSE_ID_KEY,
idValue = idValueFor(DEFAULT_CACHE_RESPONSE_ID_KEY, responseBody)
)
} else {
stubCache.addResponse(
path = resourcePath,
responseBody = responseBody,
idKey = DEFAULT_CACHE_RESPONSE_ID_KEY,
idValue = idValueFor(DEFAULT_CACHE_RESPONSE_ID_KEY, responseBody)
)
}
}

return stubCache
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -518,7 +518,7 @@ class StatefulHttpStubSeedDataFromExamplesTest {
assertThat(response.body).isInstanceOf(JSONArrayValue::class.java)

val responseBody = (response.body as JSONArrayValue)
assertThat(responseBody.list.size).isEqualTo(4)
assertThat(responseBody.list.size).isEqualTo(5)

val responseObjectFromResponseBody = (response.body as JSONArrayValue)
.list.filterIsInstance<JSONObjectValue>().first { it.getStringValue("id") == "300" }
Expand All @@ -530,7 +530,6 @@ class StatefulHttpStubSeedDataFromExamplesTest {
assertThat(responseObjectFromResponseBody.getStringValue("inStock")).isEqualTo("true")
}


@Test
fun `should get the product from seed data loaded from examples`() {
val response = httpStub.client.execute(
Expand All @@ -550,6 +549,25 @@ class StatefulHttpStubSeedDataFromExamplesTest {
assertThat(responseBody.getStringValue("inStock")).isEqualTo("true")
}

@Test
fun `should get the Samsung Ultra product from seed data loaded from examples which is a merge of request and response bodies`() {
val response = httpStub.client.execute(
HttpRequest(
method = "GET",
path = "/products/700"
)
)

assertThat(response.status).isEqualTo(200)
val responseBody = response.body as JSONObjectValue

assertThat(responseBody.getStringValue("id")).isEqualTo("700")
assertThat(responseBody.getStringValue("name")).isEqualTo("Samsung Ultra")
assertThat(responseBody.getStringValue("description")).isEqualTo("Samsung Ultra Description")
assertThat(responseBody.getStringValue("price")).isEqualTo("1000")
assertThat(responseBody.getStringValue("inStock")).isEqualTo("false")
}

@Test
fun `should not load the xiaomi product from the seed data as it has the same id (300) as that of iphone example`() {
val response = httpStub.client.execute(
Expand All @@ -563,7 +581,7 @@ class StatefulHttpStubSeedDataFromExamplesTest {
assertThat(response.body).isInstanceOf(JSONArrayValue::class.java)

val responseBody = (response.body as JSONArrayValue)
assertThat(responseBody.list.size).isEqualTo(4)
assertThat(responseBody.list.size).isEqualTo(5)

val responseObjectsFromResponseBody = (response.body as JSONArrayValue)
.list.filterIsInstance<JSONObjectValue>().filter { it.getStringValue("id") == "300" }
Expand All @@ -588,7 +606,7 @@ class StatefulHttpStubSeedDataFromExamplesTest {
assertThat(response.body).isInstanceOf(JSONArrayValue::class.java)

val responseBody = (response.body as JSONArrayValue)
assertThat(responseBody.list.size).isEqualTo(4)
assertThat(responseBody.list.size).isEqualTo(5)

val productsWithIds500And600 = (response.body as JSONArrayValue)
.list.filterIsInstance<JSONObjectValue>().filter {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"http-request": {
"path": "/products",
"method": "POST",
"headers": {
"Content-Type": "application/json"
},
"body": {
"name": "Samsung Ultra",
"price": 1000
}
},
"http-response": {
"status": 201,
"body": {
"id": 700,
"description": "Samsung Ultra Description",
"inStock": false
},
"status-text": "Created",
"headers": {
"Content-Type": "application/json"
}
}
}
Loading