Skip to content

Commit

Permalink
fix: merge pact duplicates with diff desc/same prov state
Browse files Browse the repository at this point in the history
fix: merge message / sync pacts with no provider state
  • Loading branch information
YOU54F committed Jun 8, 2024
1 parent 5699cab commit 2ad9959
Show file tree
Hide file tree
Showing 11 changed files with 1,063 additions and 59 deletions.
35 changes: 35 additions & 0 deletions python/pact_http_dupe_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
from cffi import FFI
from register_ffi import get_ffi_lib
import json
import requests

ffi = FFI()
lib = get_ffi_lib(ffi) # loads the entire C namespace
lib.pactffi_logger_init()
lib.pactffi_log_to_stdout(3)

pact = lib.pactffi_new_pact(b'merge-test-consumer', b'merge-test-provider-http')
lib.pactffi_with_specification(pact, 5)
interaction = lib.pactffi_new_interaction(pact, b'a request for an order with an unknown ID')
lib.pactffi_with_request(interaction, b'GET', b'/api/orders/404')
lib.pactffi_with_header_v2(interaction, 0,b'Accept', 0, b'application/json')
lib.pactffi_response_status(interaction, 404)

# Start mock server
mock_server_port = lib.pactffi_create_mock_server_for_transport(pact , b'0.0.0.0',0, b'http', b'{}')
print(f"Mock server started: {mock_server_port}")

try:
response = requests.get(f"http://127.0.0.1:{mock_server_port}/api/orders/404",
headers={'Content-Type': 'application/json'})
response.raise_for_status()
except requests.HTTPError as http_err:
print(f'Client request - HTTP error occurred: {http_err}') # Python 3.6
except Exception as err:
print(f'Client request - Other error occurred: {err}') # Python 3.6

result = lib.pactffi_mock_server_matched(mock_server_port)
res_write_pact = lib.pactffi_write_pact_file(mock_server_port, './pacts'.encode('ascii'), False)

## Cleanup
lib.pactffi_cleanup_mock_server(mock_server_port)
26 changes: 26 additions & 0 deletions python/pact_message_dupe_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from cffi import FFI
from register_ffi import get_ffi_lib
import json
import requests

ffi = FFI()
lib = get_ffi_lib(ffi) # loads the entire C namespace
lib.pactffi_logger_init()
lib.pactffi_log_to_stdout(3)
message_pact = lib.pactffi_new_pact(b'merge-test-consumer', b'merge-test-provider-message')
lib.pactffi_with_specification(message_pact, 5)
message = lib.pactffi_new_message(message_pact, b'an event indicating that an order has been created')
# lib.pactffi_message_expects_to_receive(message,b'Book (id fb5a885f-f7e8-4a50-950f-c1a64a94d500) created message')
# lib.pactffi_message_given(message, b'A book with id fb5a885f-f7e8-4a50-950f-c1a64a94d500 is required')
contents = {
"id": {
"pact:matcher:type": 'integer',
"value": '1'
}
}
length = len(json.dumps(contents))
size = length + 1
lib.pactffi_message_with_contents(message, b'application/json', ffi.new("char[]", json.dumps(contents).encode('ascii')), size)
reified = lib.pactffi_message_reify(message)
res_write_message_pact = lib.pactffi_write_message_pact_file(message_pact, './pacts'.encode('ascii'), False)
print(res_write_message_pact)
106 changes: 106 additions & 0 deletions python/pacts/merge-test-consumer-merge-test-provider-http.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
{
"consumer": {
"name": "merge-test-consumer"
},
"interactions": [
{
"description": "a request for an order by ID",
"pending": false,
"providerStates": [
{
"name": "an order with ID {id} exists",
"params": {
"id": 1
}
}
],
"request": {
"headers": {
"Accept": [
"application/json"
]
},
"method": "GET",
"path": "/api/orders/1"
},
"response": {
"body": {
"content": {
"date": "2023-06-28T12:13:14.0000000+01:00",
"id": 1,
"status": "Pending"
},
"contentType": "application/json",
"encoded": false
},
"headers": {
"Content-Type": [
"application/json"
]
},
"matchingRules": {
"body": {
"$.date": {
"combine": "AND",
"matchers": [
{
"match": "type"
}
]
},
"$.id": {
"combine": "AND",
"matchers": [
{
"match": "integer"
}
]
},
"$.status": {
"combine": "AND",
"matchers": [
{
"match": "regex",
"regex": "Pending|Fulfilling|Shipped"
}
]
}
}
},
"status": 200
},
"type": "Synchronous/HTTP"
},
{
"description": "a request for an order with an unknown ID",
"pending": false,
"request": {
"headers": {
"Accept": [
"application/json"
]
},
"method": "GET",
"path": "/api/orders/404"
},
"response": {
"status": 404
},
"transport": "http",
"type": "Synchronous/HTTP"
}
],
"metadata": {
"pactRust": {
"ffi": "0.4.21",
"mockserver": "1.2.8",
"models": "1.2.1"
},
"pactSpecification": {
"version": "4.0"
}
},
"provider": {
"name": "merge-test-provider-http"
}
}
86 changes: 86 additions & 0 deletions python/pacts/merge-test-consumer-merge-test-provider-message.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
{
"consumer": {
"name": "merge-test-consumer"
},
"interactions": [
{
"description": "a request to update the status of an order",
"pending": false,
"providerStates": [
{
"name": "an order with ID {id} exists",
"params": {
"id": 1
}
}
],
"request": {
"body": {
"content": "Fulfilling",
"contentType": "application/json",
"encoded": false
},
"headers": {
"Content-Type": [
"application/json"
]
},
"matchingRules": {
"body": {
"$": {
"combine": "AND",
"matchers": [
{
"match": "regex",
"regex": "Pending|Fulfilling|Shipped"
}
]
}
}
},
"method": "PUT",
"path": "/api/orders/1/status"
},
"response": {
"status": 204
},
"type": "Synchronous/HTTP"
},
{
"contents": {
"content": {
"id": "1"
},
"contentType": "application/json",
"encoded": false
},
"description": "an event indicating that an order has been created",
"matchingRules": {
"body": {
"$.id": {
"combine": "AND",
"matchers": [
{
"match": "integer"
}
]
}
}
},
"pending": false,
"type": "Asynchronous/Messages"
}
],
"metadata": {
"pactRust": {
"ffi": "0.4.21",
"models": "1.2.1"
},
"pactSpecification": {
"version": "4.0"
}
},
"provider": {
"name": "merge-test-provider-message"
}
}
55 changes: 8 additions & 47 deletions rust/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ resolver = "2"

[patch.crates-io]
onig = { git = "https://github.com/rust-onig/rust-onig", default-features = false }
pact_models = { version = "~1.2.1", path = "./pact_models" }
Loading

0 comments on commit 2ad9959

Please sign in to comment.