Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to define pact-specification matching rule for single string body?

Tags:

pact

I am setting up a test for a put request uploading a file. The request body in my pact-file consists of a single string, containing a mime boundary that changes for every test run. I am trying to define a regex matching rule for the request body string, but it won't match. A similar matching rule for the header content-type does match.

How should I define the matching rule for the body if the body is only a string?

I'm using a reference implementation of Pact in Rust. The Pact-Specification version is 3.

"request": {
    "headers": {
        "Content-Length": "206",
        "Host": "127.0.0.1:1234",
        "Connection": "Close",
        "Content-Type": "multipart/form-data; boundary=\"MIME_boundary_4FBA8D0826C707B6\""
    },
    "body": "--MIME_boundary_4FBA8D0826C707B6\r\nContent-Disposition: form-data; name=\"file\"; filename=\"test_file.txt\"\r\nContent-Type: application/octet-stream\r\n\r\nContent of test file.\r\n--MIME_boundary_4FBA8D0826C707B6--\r\n",
    "matchingRules": {
        "header": {
            "$.Content-Type": {
                "matchers": [
                    {
                        "match": "regex",
                        "regex": "multipart/form-data; boundary=\"MIME_boundary_[A-Z0-9]{16}\""
                    }
                ]
            }
        },
        "body": {
            "$": {
                "matchers": [
                    {
                        "match": "regex",
                        "regex": "--MIME_boundary_[A-Z0-9]{16}\r\nContent-Disposition: form-data; name=\"file\"; filename=\"test_file.txt\"\r\nContent-Type: application/octet-stream\r\n\r\nContent of test file.\r\n--MIME_boundary_[A-Z0-9]{16}--\r\n"
                    }
                ]
            }
        }
    }
}

The code above is part of the pact file used in the test. The test results in a BodyMismatch error. Comparing the expected and received bodies shows that they only differ in mime boundary, so the regex matching is not working.

like image 518
Renske Avatar asked Nov 06 '22 14:11

Renske


1 Answers

Through Pact's Slack channel we got the answer that the current Pact code does not support this type of matching. We created a feature request issue on GitHub: https://github.com/pact-foundation/pact-reference/issues/43

like image 69
Renske Avatar answered Dec 01 '22 14:12

Renske