0

I want to test my authorization-service via pact-jvm. In my request I'm sending a html-body via post, including some metadata to verify the user - including his username and which is also sent in the header where I use Http Basic Auth. I add the header in my testclass with @TargetRequestFilter in my JUnit-Test after reading username+password from a configuration file. This is because the application runs on different tiers. Every tier has another username+password combination. And the pact should work for every tier. Also when the user changes I only want to make little changes in my configuration file. It holds username, password, hostname, port and the protocol.

The problem is: I need to manipulate the html-body of the request depending on the content of my configuration file to match with the headers I set in my testclass without creating a new pact file every time.

So my question is: Is there a way to manipulate selective parts of the html-body I expect (via pact) from within the JUnit class?

Maybe there's another way to solve my problem I don't see yet.

Thanks in advance!


For clarity purpose an example of a Request:

<body>
    <soap:envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
        <soap:body>
            <ns2:authevalrequest1
                xmlns:ns2="http://authgroup/authBRS/specification/ServiceView/AuthProvider/authBRS/">
                <inputmetadata> <version>V_1_0_0</version> <metadataentry>
                <key>US</key> <value>some_username</value> </metadataentry> <metadataentry>
                <key>MA</key> <value>some_user_id</value> </metadataentry> </inputmetadata>
                <request> <attrs> <type>String</type> <values>
                <value>some_user_id</value> </values> <xacml>urn:oasis:names:tc:xacml:1.0:subject:subject-id</xacml>
                </attrs> <attrs> <type>String</type> <values> <value>00</value>
                </values> <xacml>http://thisisaservice.com/resource/dataRES</xacml>
                </attrs> <attrs> <type>String</type> <values> <value>abc</value>
                </values> <xacml>http://thisisaservice.com/subject/authprofilename</xacml>
                </attrs> <attrs> <type>String</type> <values> <value>importData</value>
                </values> <xacml>http://thisisaservice.com/resource/CompanyfunctionRES</xacml> </attrs>
                </request>
            </ns2:authevalrequest1>
        </soap:body>
    </soap:envelope>
</body>

I cannot change the provider or the consumer. I'm just here to make the verification work.

Edit: Maybe I forgot to say... I want to have the response bodies matching (actual and expected), what is no problem. But I need to modify the html-body of the request depending on my config-file.

KnechtRootrecht
  • 493
  • 1
  • 4
  • 18

1 Answers1

0

The problem here is that you're trying to do validation of XML, which Pact does not currently support out of the box as we do all our validation using JSON. What you can do is simply use a string validator or a regex validator, however, if anything in your string changes (like white space), your tests might not validate.

At this point, you're kind of on your own for creating a way to template the output into a string to validate your interactions. We have looked at supporting XML, but there's simply very little interest.

J_A_X
  • 12,857
  • 1
  • 25
  • 31
  • Thank you very much. I already figured out Pact does not support XML. But my problem isn't the validating. The problem is: I'm searching for a chance to modify (parts) of the body of the request I want to send to the provider. Is that even possible, when using JSON? Can I modify the JSON-data in my pact before sending them? – KnechtRootrecht Nov 28 '17 at 07:45
  • 1
    Ah, that makes more sense. This is where I would recommend that you use [provider states](https://github.com/DiUS/pact-jvm/tree/master/pact-jvm-provider-junit#example-of-http-test), then from a consumer standpoint, create several interactions for each different situation that returns a different set a data. Now within your provider, you just need to have it be able to handle the different states, which would setup the data accordingly. – J_A_X Nov 29 '17 at 04:16
  • The documentation says: "a provider state is [...] not about the state of the consumer, or about what is in the request.", so maybe I didn't really get your point or it does not solve my problem. The providers response is related to the request I send what means I need to alter the request after the pact is created while testing it against the provider. Is it somehow possible to alter the request (body) via provider states before sending it? – KnechtRootrecht Nov 29 '17 at 08:03
  • "I want to have the response bodies matching (actual and expected), what is no problem. But I need to modify the html-body of the request depending on my config-file." That config file is a state, one that isn't being communicated to pact. What you want to do is create a pact interaction for every combination possible for that config file change that affects the output, then before your test runs, the provider state should setup whatever is needed on the provider before the interaction is verified. – J_A_X Dec 04 '17 at 01:58
  • Sorry, I don't really get the point. Can you provide a somewhat practical answer? – KnechtRootrecht Dec 07 '17 at 13:12