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

Turn Pact Requests Into Printable Curl #1507

Open
francislainy opened this issue Jan 29, 2022 · 3 comments
Open

Turn Pact Requests Into Printable Curl #1507

francislainy opened this issue Jan 29, 2022 · 3 comments

Comments

@francislainy
Copy link

francislainy commented Jan 29, 2022

When Pact fails, it's a bit hard to know what the issue is considering the requests are not being printed back to the terminal. At the moment, I'm using this custom method to do this but I think it would be nice to have something like this out of the box.

public static void logCurlFromPact(PactVerificationContext context, HttpRequest request) {

        String bodyParam = ((RequestResponseInteraction) context.getInteraction()).getRequest().getBody().valueAsString();

        String bodyResponse = ((RequestResponseInteraction) context.getInteraction()).getResponse().getBody().valueAsString();

        String method = ((RequestResponseInteraction) context.getInteraction()).getRequest().getMethod();

        String url = BASE_URI + request.getPath();

        Header[] headers = request.getHeaders();

        String headersString = "";
        for (Header s : headers) {
            headersString = headersString + "--header " + "'" + s.getName() + ": " + s.getValue() + "'" + "\\" + "\n";
        }

        String curl = "" +
                "curl " +
                "--request " + method + " " +
                "'" + url + "' \\" + "\n" +
                headersString +
                "--data-binary " + "'" + bodyParam + "' \\" + "\n" +
                "--compressed \\" + "\n" +
                "--insecure \\" + "\n" +
                "--verbose" +
                "";

        log.debug(curl + "\n\n " + bodyResponse + "\n ---- \n\n");
        System.out.println(curl + "\n\n " + bodyResponse + "\n ---- \n\n");
    }

I then call it like this:


   @TestTemplate
    @ExtendWith(PactVerificationInvocationContextProvider.class)
    void pactTestTemplate(PactVerificationContext context, HttpRequest request) {

        logCurlFromPact(context, request);

        context.verifyInteraction();
    }

And that gives me the curl printed to the terminal.

image

And It also prints the response (the part under the {} )

I'd love if sth like this (or better) could be added to the library, but I'm not sure I'm the right person to take this task, so just sharing my gist as something to start with maybe?

Thanks very much.

@francislainy francislainy changed the title Print Turn Pact Requests Into Printable Curl Jan 29, 2022
@uglyog
Copy link
Member

uglyog commented Jan 31, 2022

This is an awesome feature!

@PostIt59
Copy link

Amazing feature ! Indeed, it will help us a lot 👍

@YOU54F
Copy link
Member

YOU54F commented Jun 25, 2024

Hey @francislainy,

Going through some old issues and this is very cool!

If you are still around in the Pact world, would you want to consider turning this into a pull request so it could become a Pact feature?

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

5 participants