Hi all, Are there any ways to publish consumer pa...
# pact-net
b
Hi all, Are there any ways to publish consumer pact files to Azure BlobStorage and then verify them on provider side? :-)
t
Yes, you can just publish the files that are written by pact, and then download them for verification on the provider side
You’re much better off using a broker, though, as it remembers what was compatible, and knows what is deployed where
b
Thanks a lot for your reply, Timtohy, it’s greatly appreciated 😊 You mean publish the files manually and download them manually? I meant automatically 😃 sorry that i was not explicit around that
t
Yep, manually. The way to do it automatically, built in, is to use a broker
The reason there is no other tooling built is that a significant portion of the value of contract testing comes from using the broker for deployment reasoning
There’s an open source one already in a docker container if you want to run your own, or if you want to not have to build that infrastructure, you can use the SaaS one that the fine people at Pactflow run
https://hub.docker.com/r/pactfoundation/pact-broker ^ Free docker image https://pactflow.io/ ^ Paid SaaS broker (though the free tier is fine if you’re just doing a PoC)
Is there some reason you want to use Azure BlobStorage over a broker?
y
You have to build out all the pact broker logic, so although you can we don't spend much showcasing it and there isn't much value in supporting it from the maintainers perspective https://docs.pact.io/pact_broker#can-i-use-pact-without-a-pact-broker https://docs.pact.io/getting_started/sharing_pacts#4-publish-pacts-to-amazon-s3 https://docs.pact.io/implementation_guides/jvm/provider/gradle#verifying-pact-files-from-a-s3-bucket we have taken the time to show some options however
b
Thanks for your replies, Timothy and Yousaf 🙂 🙏 The reason I wanted to use Azure BlobStorage for storing the pacts was for a simple proof of concept, since I'm working for a world-wide business where we have teams all over the world (and thus microservices) where we want to try out PACT for contract testing. I've registered a free trial for pactflow now, though 🙂 While I realize that we should use the CLI in our CI/CD pipelines to publish the pacts, I want to quickly spin up a new
HttpClient
in .net to publish the contracts, just to get started. However, I get a 400 bad request when I try pondering My code looks like this:
Copy code
byte[] jsonBytes = File.ReadAllBytes("../../../../../pacts/mypactfile.json");
        var base64 = Convert.ToBase64String(jsonBytes);

        dynamic payload = new
        {
            pacticipantName = "my participant name",
            pacticipantVersionNumber = 1,
            contracts = new dynamic[]
            {
                new
                {
                    consumerName = "my consumer",
                    providerName = "my provider",
                    specification = "pact",
                    contentType = "application/json",
                    content = base64
                }
            }
        };

        using var httpClient = new HttpClient();
        httpClient.BaseAddress = new Uri("<https://myaccount.pactflow.io/>");

        HttpRequestMessage message = new(<http://HttpMethod.Post|HttpMethod.Post>, "contracts/publish");
        message.Headers.Authorization = new AuthenticationHeaderValue("Bearer", "my api token from <http://pactflow.io|pactflow.io>");
        message.Content = new StringContent(JsonSerializer.Serialize(payload), Encoding.UTF8, "application/json");

        HttpResponseMessage responseMessage = httpClient.Send(message);
        if (!responseMessage.IsSuccessStatusCode)
        {
            throw new Exception($"Failed to publish pacts. Status code: {responseMessage.StatusCode}");
        }
Any idea of what I did wrong here? :-)
t
new AuthenticationHeaderValue(“Bearer”, “my api token from pactflow.io”);
I don’t know what this does specifically, but in case you hand rolled it, the header must be
Bearer YOURTOKEN
(note the space) Otherwise yours looks ok. In mine, I also have
tags: []
, and
Accept: 'application/json'
.
Ah, sorry - I think it must be
Accept: 'application/hal+json',
(it’s in JS, but hopefully you can use it to compare)
b
Thanks Timothy 🙂 the AuthenticationHeaderValue is just a standard way to authorize via the http client in .net. It actually works the way I did it 😄 I found the error.. it was a stupid one: name mismatch between the provider name in the contract and what I wrote in the publish method 🤯 😆
t
Hah! I didn’t know it checked the contract contents
If you figure out how to get the detailed verification results publishing, let me know. I haven’t been able to figure that one out and it isn’t documented
I also found some of the endpoints don’t work unless you accept
application/hal+json
b
It actually works just by setting
application/json
, with out the
hal+json
🙂
t
Nice. Not all of them do this, though I think they produce an appropriate error code when they fail
b
Fetched the error phrase like this, which told me exactly what was wrong:
Copy code
HttpResponseMessage responseMessage = httpClient.Send(message);
        if (!responseMessage.IsSuccessStatusCode)
        {
            string error = await responseMessage.Content.ReadAsStringAsync();
            throw new Exception($"{error}. Status code: {responseMessage.StatusCode}");
        }
t
Also the bodies of the response are pretty good
yeah
b
yea, they are indeed 🙂 👍
Kodus to the people behind PACT.NET! Setting up pactflow provider verification was a bliss 😄
chefkiss 1
🙌 1
t
Yep! It’s definitely the easiest way