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

Ending a state definition #497

Closed
Nalha opened this issue Jul 26, 2017 · 8 comments
Closed

Ending a state definition #497

Nalha opened this issue Jul 26, 2017 · 8 comments
Labels
bug Indicates an unexpected problem or unintended behavior

Comments

@Nalha
Copy link

Nalha commented Jul 26, 2017

In pact-jvm-consumer-junit (and others?) there is currently no way to end a "given" definition, this makes it easy to accidentally place more requests that should'nt be in that state.
One way around this is using states for all requests but I don't think that should feel as forced as it currently is.

.given("mystate")
    .uponReceiving("post my request")
        .matchPath("/api/.+/.+/.+")
        .method("POST")
    .willRespondWith()
        .status(200)
        .body("string result")`
.endGiven()                       <---
    .uponReceiving...
@uglyog
Copy link
Member

uglyog commented Aug 2, 2017

The state only applies to the proceeding interaction. The next .uponReceiving call resets the state to none if there was not another .given that preceded it.

So

    .given("mystate")
    .uponReceiving("post my request") // This interaction has state 'mystate'
        .matchPath("/api/.+/.+/.+")
        .method("POST")
    .willRespondWith()
        .status(200)
        .body("string result")`
    .uponReceiving... // This interaction will have no state, as it resets the state and there was no preceeding .given call

@Nalha
Copy link
Author

Nalha commented Aug 2, 2017

That might be the idea but it is not how it currently works, in consumer-junit 3.5.2 all 3 uponReceiving following my test state got the following in the JSON:

            "providerStates": [
                {
                    "name": "test state"
                }
            ]

@uglyog
Copy link
Member

uglyog commented Aug 3, 2017

That's a defect then.

@uglyog uglyog added the bug Indicates an unexpected problem or unintended behavior label Sep 9, 2017
@solarmosaic-kflorence
Copy link

@uglyog I just saw this as well, with version 4.2.8 of pact-jvm:

  ConsumerPactBuilder
    .consumer(consumerName)
    .hasPactWith(providerName)
    .given("greeting", Map("name" -> "world").asJava)
    .uponReceiving("GET /hello")
    .path("/hello")
    .method("POST")
    .willRespondWith()
    .status(200)
    .body(Json.toJson(HelloResponse("world")).toString())
    .uponReceiving("GET /hello-user")
    .path("/hello-user")
    .method("POST")
    .willRespondWith()
    .status(200)
    .body(Json.toJson(HelloResponse("user")).toString())
    .toPact()

Results in the following pact file:

{
  "consumer": {
    "name": "consumer"
  },
  "interactions": [
    {
      "description": "GET /hello",
      "providerStates": [
        {
          "name": "greeting",
          "params": {
            "name": "world"
          }
        }
      ],
      "request": {
        "method": "POST",
        "path": "/hello"
      },
      "response": {
        "body": {
          "hello": "world"
        },
        "status": 200
      }
    },
    {
      "description": "GET /hello-user",
      "providerStates": [
        {
          "name": "greeting",
          "params": {
            "name": "world"
          }
        }
      ],
      "request": {
        "method": "POST",
        "path": "/hello-user"
      },
      "response": {
        "body": {
          "hello": "user"
        },
        "status": 200
      }
    }
  ],
  "metadata": {
    "pact-jvm": {
      "version": "4.2.8"
    },
    "pactSpecification": {
      "version": "3.0.0"
    }
  },
  "provider": {
    "name": "example"
  }
}

@solarmosaic-kflorence
Copy link

The problem seems to be here:

return PactDslRequestWithPath(consumerPactBuilder, request!!, description, defaultRequestValues,

It is starting a new interaction using the previous PactDslRequestWithPath, which carries over the consumer, provider, state and path.

@uglyog
Copy link
Member

uglyog commented Jul 31, 2021

@solarmosaic-kflorence good catch!

@uglyog
Copy link
Member

uglyog commented Aug 1, 2021

Released 4.1.24 and 4.2.9

@solarmosaic-kflorence
Copy link

@uglyog confirmed:

{
  "consumer": {
    "name": "consumer"
  },
  "interactions": [
    {
      "description": "GET /hello-user",
      "request": {
        "method": "POST",
        "path": "/hello-user"
      },
      "response": {
        "body": {
          "hello": "user"
        },
        "status": 200
      }
    },
    {
      "description": "GET /hello",
      "providerStates": [
        {
          "name": "greeting",
          "params": {
            "name": "world"
          }
        }
      ],
      "request": {
        "method": "POST",
        "path": "/hello"
      },
      "response": {
        "body": {
          "hello": "world"
        },
        "status": 200
      }
    }
  ],
  "metadata": {
    "pact-jvm": {
      "version": "4.2.9"
    },
    "pactSpecification": {
      "version": "3.0.0"
    }
  },
  "provider": {
    "name": "example"
  }
}

I think this ticket can be closed.

@uglyog uglyog closed this as completed Aug 2, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Indicates an unexpected problem or unintended behavior
Projects
None yet
Development

No branches or pull requests

3 participants