Pact Provider with Authorization Header doesn't work?

935 views
Skip to first unread message

Chuan Chuan Law

unread,
Nov 26, 2015, 7:10:05 PM11/26/15
to Pact
Hi,

I got a scenario where a GET request needs to set Authorization Header.

I tried the following methods:

(1) Adding Authorization Header in consumer test

(2) Adding  Authorization Header in requestFilter in Pact Provider 

But, both returns a 403 when we run the Provider test.

The API works fine if we call it via tools like Postman.

Please advice.


Thank you.

Ronald Holshausen

unread,
Nov 26, 2015, 7:35:23 PM11/26/15
to Chuan Chuan Law, Pact
In what form is the authorization header, basic authentication or some type of token?


--
You received this message because you are subscribed to the Google Groups "Pact" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pact-support...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Ronald Holshausen

DiUS Computing Pty Ltd

Level 10, 99 Queens Street
Melbourne, VIC 3000

Phone: +61 3 9008 5400
Mobile: +61 413 162 439

http://www.diuscomputing.com.au

Chuan Chuan Law

unread,
Nov 26, 2015, 7:39:05 PM11/26/15
to Pact

Its the basic one, which we pass in as Header.

Ronald Holshausen

unread,
Nov 26, 2015, 7:47:43 PM11/26/15
to Chuan Chuan Law, Pact
Basic authentication headers need to be Base64 encoded and in the form "Basic <BASE64ENCODEDDATA>". You can get the value from Postman when you enter your login details and select update request. Then display the authorization header in the headers tab and copy the value exactly.

Chuan Chuan Law

unread,
Nov 26, 2015, 7:55:26 PM11/26/15
to Pact
Hi Ronald,


Thanks for your reply.

But that's not the type of Authorization I mean.

It's the type which falls under the Header section, with the Header name 'Authorization' and a value in a form of a String.

When calling via Postman, the Authorization tab is left empty, just need to fill in the Headers tab.


Cheers

Ronald Holshausen

unread,
Nov 26, 2015, 8:16:24 PM11/26/15
to Chuan Chuan Law, Pact
The Authorization tab generates a 'Authorization' header in the Header section. But if you have a working header you are adding, can you make sure the contents are exactly the same in postman as is in the requestFilter in Pact Provider.

Chuan Chuan Law

unread,
Nov 26, 2015, 9:59:29 PM11/26/15
to Pact

I did......but still get getting 403. :(

Any other possibility?

Ronald Holshausen

unread,
Nov 26, 2015, 10:05:08 PM11/26/15
to Chuan Chuan Law, Pact
It could be there are some other headers also required. Make sure that all headers are the some as in the postman request, and check that spelling and values are correct. I can't think of any other things that could be impacting it.

If you can, contact the people responsible for the API and see if they can provide some logs.

Ronald Holshausen

unread,
Nov 26, 2015, 10:07:48 PM11/26/15
to Chuan Chuan Law, Pact
If you run the gradle command with -i, it will print out more information including the requests and response headers. Check that the authentication header is set correctly.

Chuan Chuan Law

unread,
Nov 26, 2015, 10:55:22 PM11/26/15
to Pact
Ok....will try..thanks heaps Ronald.

Cheers,

Chuan

Chuan Chuan Law

unread,
Nov 30, 2015, 12:52:01 AM11/30/15
to Pact, chuan.c...@gmail.com
Below is the copy of the log running with --debug option, which I cannot find the authentication header being set:


16:45:10.217 [QUIET] [system.out]   a Get request
16:45:10.573 [DEBUG] [org.apache.http.client.protocol.RequestAddCookies] CookieS
pec selected: default
16:45:10.584 [DEBUG] [org.apache.http.client.protocol.RequestAuthCache] Auth cac
he not set in the context
16:45:10.585 [DEBUG] [org.apache.http.impl.conn.PoolingHttpClientConnectionManag
er] Connection request: [route: {}->http://app.livemarket.trade.csprd.com.au:80]
[total kept alive: 0; route allocated: 0 of 2; total allocated: 0 of 20]
16:45:10.602 [DEBUG] [org.apache.http.impl.conn.PoolingHttpClientConnectionManag
er] Connection leased: [id: 0][route: {}->http://app.livemarket.trade.csprd.com.
au:80][total kept alive: 0; route allocated: 1 of 2; total allocated: 1 of 20]
16:45:10.603 [DEBUG] [org.apache.http.impl.execchain.MainClientExec] Opening con
16:45:10.611 [DEBUG] [org.apache.http.impl.conn.DefaultHttpClientConnectionOpera
16:45:10.643 [DEBUG] [org.apache.http.impl.conn.DefaultHttpClientConnectionOpera
tor] Connection established 10.1.21.62:62049<->202.58.52.221:80
16:45:10.643 [DEBUG] [org.apache.http.impl.execchain.MainClientExec] Executing r
equest GET /integration/internal/snapshot-simple? HTTP/1.1
16:45:10.643 [DEBUG] [org.apache.http.impl.execchain.MainClientExec] Proxy auth
state: UNCHALLENGED
16:45:10.711 [DEBUG] [org.apache.http.impl.execchain.MainClientExec] Connection
can be kept alive for 5000 MILLISECONDS
16:45:10.752 [DEBUG] [org.apache.http.impl.conn.PoolingHttpClientConnectionManag
er] Connection [id: 0][route: {}->http://app.livemarket.trade.csprd.com.au:80] c
an be kept alive for 5.0 seconds
16:45:10.752 [DEBUG] [org.apache.http.impl.conn.PoolingHttpClientConnectionManag
er] Connection released: [id: 0][route: {}->http://app.livemarket.trade.csprd.co
m.au:80][total kept alive: 1; route allocated: 1 of 2; total allocated: 1 of 20]




On Friday, 27 November 2015 14:07:48 UTC+11, Ronald Holshausen wrote:

Ronald Holshausen

unread,
Nov 30, 2015, 6:03:32 PM11/30/15
to Chuan Chuan Law, Pact
I don't know why the debug is not displaying anything, but the headers are definitely being passed through.

I added the following to my test project:

pact {
serviceProviders {
sampleprovider {
port = 5050

requestFilter = { req ->
// Add an authorization header to each request
req.addHeader('Authorization', 'OAUTH eyJhbGciOiJSUzI1NiIsImN0eSI6ImFw...')
}

hasPactWith("sampleconsumer") {
pactFile = file('src/test/resources/sample-pact.json')
}
hasPactWith("sampleconsumer2") {
pactFile = file('src/test/resources/sample-pact2.json')
}
}
}
}
and then on the provider I dumped the headers:

[main] INFO ratpack.server.RatpackServer - Ratpack started (development) for http://localhost:5050

'Accept'=application/json
'Accept-Encoding'=gzip,deflate
'content-length'=0
'Authorization'=OAUTH eyJhbGciOiJSUzI1NiIsImN0eSI6ImFw...
'Connection'=Keep-Alive
'Host'=localhost:5050
'Content-Type'=application/json
'User-Agent'=Apache-HttpClient/4.5.1 (Java/1.8.0_66)

Chuan Chuan Law

unread,
Dec 1, 2015, 12:08:32 AM12/1/15
to Pact

Provider version is the problem. I was using version 2.11:3.0.1. Upgrading to 2.11:3.2.0 works.

Thanks for your help Ronald.
Reply all
Reply to author
Forward
0 new messages