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

Content-Type matching is too strict / doesn't follow matcher specification #406

Open
alexeits opened this issue Mar 28, 2024 · 2 comments
Open

Comments

@alexeits
Copy link

alexeits commented Mar 28, 2024

Given the pact JSON with term matcher on Content-Type header:

"response": {
  "status": 200,
  "headers": {
    "Content-Type": "text/plain"
  },
  "body": "fake-header.fake-token.fake-signature",
  "matchingRules": {
    "$.headers.Content-Type": {
      "match": "regex",
      "regex": "^text\\/plain"
    },
    "$.body": {
      "match": "regex",
      "regex": "^[^.]+.[^.]+.[^.]+$"
    }
  }
}

Complete JSON: demo-loader-auth.json

and the provider responding with

HTTP/1.1 200 OK
Connection: close
Content-Type: text/plain; charset=utf-8

eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE3MTE2NTEyMjEsImlhdCI6MTcxMTY0NDAyMSwidG9waWNQZXJtaXNzaW9ucyI6ImVKeUtqZ1VFQUFELy93RVZBTGs9IiwidXNlcklkIjoic2FnYW5BZG1pbiIsIm9yZ0lkIjoic2FnYW4iLCJyb2xlSWRzIjpbInNhZ2FuLWFkbWluIl0sInNhZ2FuVXNlciI6InNhZ2FuLW9wZXJhdG9yIiwic2Vzc2lvbiI6eyJpZCI6IiJ9fQ.GaDrAz1132zOptIyElUp8-vSUkTZn47lFTGrzBKSycw

which matches Content-Type regex: https://regex101.com/r/eaiKgC/2

the corresponding pactffi trace:

RequestResponseInteraction { id: None, description: "create admin token for valid operator token", provider_states: [ProviderState { name: "operator has valid token", params: {} }], request: Request { method: "POST", path: "/api/v1/admin_tokens", query: None, headers: Some({"Content-Type": ["application/json"]}), body: Present(b"{\"token\":\"1234567890abcdef1234567890abcdef12345678\",\"username\":\"sagan-operator\"}", None, None), matching_rules: MatchingRules { rules: {} }, generators: Generators { categories: {} } }, response: Response { status: 200, headers: Some({"Content-Type": ["text/plain"]}), body: Present(b"fake-header.fake-token.fake-signature", Some(ContentType { main_type: "text", sub_type: "plain", attributes: {}, suffix: None }), None), matching_rules: MatchingRules { rules: {HEADER: MatchingRuleCategory { name: HEADER, rules: {DocPath { path_tokens: [Root, Field("Content-Type")], expr: "Content-Type" }: RuleList { rules: [Regex("^text\\/plain")], rule_logic: And, cascaded: false }} }, BODY: MatchingRuleCategory { name: BODY, rules: {DocPath { path_tokens: [Root], expr: "$" }: RuleList { rules: [Regex("^[^.]+.[^.]+.[^.]+$")], rule_logic: And, cascaded: false }} }} }, generators: Generators { categories: {} } } }

the verification fails with:

5) Verifying a pact between demo-loader and auth Given operator has valid token - create admin token for valid operator token
    5.1) includes header 'Content-Type' with value 'text/plain'
           Expected header 'Content-Type' to have value 'text/plain' but was 'text/plain; charset=utf-8'

This issue is present in libpact_ffi 0.4.16.

@rholshausen
Copy link
Contributor

Just change your regex to ^text/plain(;.*)? and it will match

@alexeits
Copy link
Author

@rholshausen TYVM for the tip. I confirm that it works.

Still arguably the behavior is unexpected because normally ^text/plain regex should match all strings that start with that prefix.

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