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

How to match a non-json format respond body? #81

Open
YingzheHeBetty opened this issue Nov 9, 2020 · 11 comments
Open

How to match a non-json format respond body? #81

YingzheHeBetty opened this issue Nov 9, 2020 · 11 comments

Comments

@YingzheHeBetty
Copy link

Hi,
I have a pact file like this:

{
  "consumer": {
    "name": "CMM"
  },
  "provider": {
    "name": "Analytics Service"
  },
  "interactions": [
    {
      "description": "a get request for Creatives Metadata Report",
      "providerState": "agency id 25342d03-d07f-4047-8887-5f671f3082d6 exist",
      "request": {
        "method": "get",
        "path": "/v1/cloud_ingestion/agencies/25342d03-d07f-4047-8887-5f671f3082d6/reports/metadata/creatives",
        "query": ""
      },
      "response": {
        "status": 200,
        "headers": {
          "Content-Type": "text/csv"
        },
        "body": "id,external_ref,external_source,name,media_type,pixel_size",
        "matchingRules": {
          "$.body": {
            "match": "type"
          }
        }
      }
    }
  ],
  "metadata": {
    "pactSpecification": {
      "version": "2.0.0"
    }
  }
}

When I verified it on the provider side,
the test failed:

`1) a get request for Creatives Metadata Report

    1.1) body: / Actual body 'id,external_ref,external_source,name,media_type,pixel_size
' is not equal to the expected body 'id,external_ref,external_source,name,media_type,pixel_size'



java.lang.AssertionError: 
Failures:

1) a get request for Creatives Metadata Report

    1.1) body: / Actual body 'id,external_ref,external_source,name,media_type,pixel_size
' is not equal to the expected body 'id,external_ref,external_source,name,media_type,pixel_size'

`

Any solutions? Thank you!

@bethesque
Copy link
Member

CSV matching is not supported (except in Pact Ruby). You can only do exact string matching unfortunately.

@uglyog
Copy link
Member

uglyog commented Nov 10, 2020

I think this is a valid concern, so re-opening

@uglyog uglyog reopened this Nov 10, 2020
@uglyog
Copy link
Member

uglyog commented Nov 10, 2020

This highlights a deeper issue, how can we provide an extension mechanism for formats that don't have a default implementation. Maybe shared libraries, or possibly an embedded scripting language so that support for other formats can be easily added.

@bethesque
Copy link
Member

The ruby one allows you to configure a differ based on the content type, so you can write your own. That's why it has the CSV support.

@uglyog
Copy link
Member

uglyog commented Nov 10, 2020

I assume that needs to be written in Ruby and made available to the underlying Ruby engine. That you can't write it in the language being used? I was thinking of something like an API that allows extensions to be added in a generic way.

@mefellows
Copy link
Member

I looked into this when spiking protobufs. Golang has very good support for cross platform plugins, and I showed that I could integrate that into rust as a lib. But then I just thought about the complexitybthay might result it.

A possibly better alternative is to have the plugin expose an well defined API interface and register with the system. This would have the upside of allowing anyone to write a plugin in any language and would require them contributing it back (the downside is they will be written in many languages 😂)

@YingzheHeBetty
Copy link
Author

YingzheHeBetty commented Nov 10, 2020

The ruby one allows you to configure a differ based on the content type, so you can write your own. That's why it has the CSV support.

Could you provide more details/examples on CSV support?
In my case, my consumer is written in ruby and my provider is written in JVM. Is there any way that I could verify CSV format response body?

@bethesque
Copy link
Member

I haven't touched it for years, but here are the docs https://www.rubydoc.info/gems/pact-csv/0.0.1

@YingzheHeBetty
Copy link
Author

I haven't touched it for years, but here are the docs https://www.rubydoc.info/gems/pact-csv/0.0.1

@YingzheHeBetty
Copy link
Author

Is it possible to verify non-json format response body? If my provider is written in Java?

This highlights a deeper issue, how can we provide an extension mechanism for formats that don't have a default implementation. Maybe shared libraries, or possibly an embedded scripting language so that support for other formats can be easily added.

It would be very useful to have it added into pact testing!

@uglyog
Copy link
Member

uglyog commented Oct 20, 2021

A prototype CSV plugin has been written. See https://github.com/pact-foundation/pact-plugins/tree/main/plugins/csv

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

4 participants