pact-foundation / pact-net

.NET version of Pact. Enables consumer driven contract testing, providing a mock service and DSL for the consumer project, and interaction playback and verification for the service provider project.
https://pact.io
MIT License
823 stars 225 forks source link

Pact request body with inner object gives internal server error in Pact Net #494

Closed cetk closed 4 months ago

cetk commented 4 months ago
_mockProviderService.UponReceiving("a request to create a new event")
    .With(new ProviderServiceRequest{
        Method = HttpVerb.Post, Path = "/events",
        Headers =
            new Dictionary<string, object>{
                {"Content-Type", "application/json; charset=utf-8"}},
        Body =
            new {eventId, eventType = "DetailsView",
                 timestamp = new TimeObject{date = "2024-02-25 21:38:57.290"}}})
    .WillRespondWith(new ProviderServiceResponse{Status = 201});
mefellows commented 4 months ago

I don't believe you can just pass objects to the DSL arbitrarily, unless they have an implicit conversion to a JSON type. The input to the interaction needs to map to the payload that will actually be sent over the wire.

NOTE: you can use the code gates (```) to better format your code and aid with readability. I have updated your question to do that now.

cetk commented 4 months ago

Thanks for reply. In java, we have PactDslJsonBody which I believe is extension to DslBody in which we can pass arrays or objects like this - private val postRequestBody: PactDslJsonBody = PactDslJsonBody() .array("events") .uuid(currentUserId.toString()) .uuid(participantId.toString()) .closeArray().asBody() .array("eventItems") .object() .stringValue("title", "Event Item 1") .stringValue("notes", "Notes for item 1") .booleanType("editable", true) .closeObject() .closeArray().asBody() .and("title", "Event Title") .object("schedule") .stringType("startAt", "2023-01-19T12:41:41.111503Z") .stringType("endAt", "2023-02-19T12:41:41.111503Z") .closeObject() .asBody()

can be used as @Pact(consumer = "Event-api-consumer", provider = "Event-service-conversations") fun postEventPact(builder: PactDslWithProvider): RequestResponsePact { return builder .given("user exists") .uponReceiving("a request to create a event") .path("/$BASE_URL$EVENTS_PATH") .method("POST") .headers(requestHeaders) .body(postRequestBody) .willRespondWith() // .headers(headers) .status(HttpStatus.SC_CREATED) .body(postResponseBody) .toPact() }

I am looking for .NET version for PactDslJsonBody

cetk commented 4 months ago

Adding to above, I am using .NET framework 4.6.2, don't have liberty to upgrade to latest version for .NET framework or .NET Core. I may upgrade PactNet if it will resolve above error.

adamrodger commented 4 months ago

This is using PactNet 3.x or below, which are deprecated versions. Please update to PactNet 4.x.

mefellows commented 4 months ago

See also the docs for the 3.x.x line (including working examples) here: https://github.com/pact-foundation/pact-net/tree/3.0.2

cetk commented 4 months ago

Thanks for reply. I understand upgrading the version but my project uses .NET framework 4.6.1 and I believe the latest PactNet version doesn't support this .NET framework.