how to use web mock and open-uri authentication ?

342 views
Skip to first unread message

guillot nicolas

unread,
Oct 11, 2013, 12:28:26 PM10/11/13
to webmoc...@googlegroups.com
Hi

there is the open-uri method :

    def self.open_url(url, user, password)

        begin

            return open(url, :http_basic_authentication=>[user, password], "Accept" => "application/xml").read

        rescue  Exception=>e

            #p e.message

            puts "Failed to parse url : #{url}"

            return "<failed/>"

        end

    end



And my test method (web mock/respec) content:

        @user = "login"

        @pwd = "pwd"

        @base64_code = Base64.encode64("#{@user}:#{@pwd}").delete("\n")


        uri = "https://api.appannie.com/v1/accounts/blabla"

        raw_response_file = load_fixture("app_annie_ok.txt")

        stub_request(:get, uri).with(:headers => {"Authorization" => "Basic "+ @base64_code}).to_return(raw_response_file)


I also try:

        uri = "#{@user}:#{@pwd}@api.appannie.com/v1/accounts/blabla"

        raw_response_file = load_fixture("app_annie_ok.txt")

        stub_request(:get, uri).with(:headers => {"Authorization" => "Basic "+ @base64_code}).to_return(raw_response_file)


All without success.


Anyone have an idea ?


Thanks

Bartosz Blimke

unread,
Oct 12, 2013, 8:45:51 AM10/12/13
to webmoc...@googlegroups.com
The basic authentication user and password should be added to the uri in stub declaration.
Please have a look at "Request with basic authentication" example in README.

Alternatively just execute the request with no stub at all, and WebMock should print the suggested stub declaration to handle this request.

Bartosz


2013/10/11 guillot nicolas <guillot...@gmail.com>

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

guillot nicolas

unread,
Oct 14, 2013, 7:13:50 AM10/14/13
to webmoc...@googlegroups.com
Thanks a lot it works !
Reply all
Reply to author
Forward
0 new messages