Trying to mock an api call with HMAC headers

52 views
Skip to first unread message

Brett Minnie

unread,
Nov 5, 2015, 10:09:56 AM11/5/15
to webmock-users
Hi All,

New to ruby and webmock, I have had to integrate my api with HMAC auth, have done this via the api-auth gem, the calls are working perfectly fine, and my Symfony api is responding correctly.

I am trying to mock the api calls now that I have implemented the authentication and have the following issue, I have 3 headers that will not have a preset value and have tried wildcard these

I keep getting the following error

WebMock::NetConnectNotAllowedError: Real HTTP connections are disabled. Unregistered request: PUT http://localhost:8000/api/about-you/feeling/0 with body 'energy&fitness&stress&fatigue&weight&mood' with headers {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'Authorization'=>'APIAuth 4e9abbfb9ccb12b72025d44c4c7d6c5a8d428c2c:+bCjS4EhZQ/m3njttKhSAhwUBfs=', 'Content-Md5'=>'f44e2ea4d73b1482e12e10a77f8a5e06', 'Content-Type'=>'application/x-www-form-urlencoded', 'Date'=>'Thu, 05 Nov 2015 14:47:23 GMT', 'User-Agent'=>'Ruby'}

my test for the following looks like so, the regex's match when tested against the relevant portions in regexr, so not sure if what I am trying is possible


test "should put what_hinders_you" do
  WebMock::stub_request(:put, "http://127.0.0.1:8000/api/about-you/feeling/0").
      with(:body => {"energy"=>nil, "fatigue"=>nil, "fitness"=>nil, "mood"=>nil, "stress"=>nil, "weight"=>nil},
         :headers => {
           'Accept'=>'*/*',
           'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
           'Authorization'=> /\w+ [a-f0-9]+:(\w|.)+/,
           'Content-Md5'=> /[a-f0-9]+/,
           'Content-Type'=>'application/x-www-form-urlencoded',
           'Date'=> /^\w+, \d{1,2} \w+ \d{4} \d+:\d+:\d+ GMT/,
           'User-Agent'=>'Ruby'
      }
   ).
   to_return(:status => 200, :body => questionnaire.to_json, :headers => {})

  post(:what_hinders_you, {feeling_id: 0})
  assert_response :success
end

Thanks for any pointers

Brett

Brett Minnie

unread,
Nov 5, 2015, 10:21:14 AM11/5/15
to webmock-users
Managed to simply the test down to the following so it now works:

test "should put what_hinders_you" do
  WebMock::stub_request(:put, \about-you\/feeling\).
   and_return(:status => 200, :body => questionnaire.to_json, :headers => {})
Reply all
Reply to author
Forward
0 new messages