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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Running pact test with a matcher in request body returns a Server Error from mock server #78

Closed
surpher opened this issue Oct 13, 2020 · 2 comments

Comments

@surpher
Copy link

surpher commented Oct 13, 2020

馃寧 Environment

  • Platform: PactSwift
  • pact-reference: libpact_mock_server_ffi built from master (cargo build --release)

馃挰 Description

When writing a pact test where the request body includes a matcher, the response from mock server is Server Error, status code: 500.

Update:
Happens with POST and request body without a matcher too, but when I used PUT and passed a a request body without a matcher, I received a 201 response as expected.
No change even when specifically defining the request's expected headers to be Content-Type: application/json.

馃Χ Reproduction Steps

Steps to reproduce the behavior:

  1. Prepare a unit test that generates a Pact object where a matcher is in the request body
  2. Pass Pact object to libpact_mock_server_ffi
  3. Trigger network request to test the interaction

馃 Expected Results

The response from mock server returns the value provided in the matcher and status code as defined in pact test.

馃槻 Actual Results

The response from mock server returns 500 - Server Error.

馃尦 Logs

Pact object sent to libpact_mock_server_ffi.create_mock_server()

{
	"metadata": {
		"PactSwift": { "version": "0.0.0" },
		"pactSpecification": { "version": "3.0.0" }
	},
	"interactions": [{
		"response": {
			"status": 201
		},
		"providerState": "users does not exist",
		"request": {
			"path": "\/user",
			"body": {
				"name": "Joe"
			},
			"method": "post",
			"matchingRules": {
				"body": {
					"$.name": {
						"matchers": [{
							"match": "type"
						}]
					}
				}
			}
		},
		"description": "Request to create a user"
	}],
	"provider": { "name": "unit-test-api-provider" },
	"consumer": { "name": "pactswift-unit-tests" }
}

馃搫 Stack Traces

n/a

馃 Relationships

  • Related PRs or Issues:
@surpher
Copy link
Author

surpher commented Oct 14, 2020

Update, perhaps it has to do with existing Pact file in PACT_OUTPUT_DIR?
Or maybe having multiple tests setup with same path, query and body but different method?

The following test written in PactSwift passes just fine:

    func testMockService_Succeeds_ForPOSTRequestWithMatchersInRequestBody() {
		mockService
			.uponReceiving("Request to create a new user")
			.given("user does not exist")
			.withRequest(
				method: .POST,
				path: "/user/add",
				query: nil,
				headers: ["Content-Type": "application/json"],
				body: [
					"name": Matcher.SomethingLike("Joe"),
				        "age": Matcher.IntegerLike(42)
				 ]
			)
			.willRespondWith(
				status: 201
			)

		let testExpectation = expectation(description: #function)

		mockService.run { completion in
			let requestURL = URL(string: "\(self.mockService.baseUrl)/user/add")!
			let session = URLSession.shared
			var request = URLRequest(url: requestURL)

			request.httpMethod = "POST"
			request.setValue("application/json", forHTTPHeaderField: "Content-Type")
			request.httpBody = #"{"name":"Joseph","age":24}"#.data(using: .utf8)

			let task = session.dataTask(with: request) { data, response, error in
				if let response = response as? HTTPURLResponse {
					XCTAssertEqual(response.statusCode, 201)
				}
				testExpectation.fulfill()
				completion()
			}
			task.resume()
		}

		waitForExpectations(timeout: 1)
	}

@surpher surpher closed this as completed Oct 14, 2020
@uglyog
Copy link
Member

uglyog commented Oct 18, 2020

This might be affected by #77, if there was an existing pact file, it would not have been merged correctly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants