Setting the HTTP response headers?

117 views
Skip to first unread message

David Michael

unread,
Jun 9, 2009, 11:03:55 AM6/9/09
to FakeWeb
Hello

How do you set the HTTP response headers of the faked response?

Thanks
David

Ben Schwarz

unread,
Jun 11, 2009, 11:48:24 PM6/11/09
to FakeWeb
David,

You can record your responses using curl on the command line:

curl -i http://domain.tld > spec/support/domain.tld.curl

Then using fakeweb you can do:

FakeWeb.register_uri(@url, :file => File.join(SPEC_DIR, 'support',
'domain.tld.curl'))

However you cannnot pass HTTP headers through fakeweb.
Ideally I'm looking to do something like:

FakeWeb.register_uri(@url, :file => File.join(SPEC_DIR, 'support',
'domain.tld.curl'), :content_encoding => "gzip")

I sent Chris a message on github, I'll be patently awaiting his
response :)


Cheers,

Ben

Chris Kampmeier

unread,
Jun 14, 2009, 4:58:30 AM6/14/09
to FakeWeb
On Jun 11, 8:48 pm, Ben Schwarz <ben.schw...@gmail.com> wrote:
> You can record your responses using curl on the command line:
>
> curl -ihttp://domain.tld> spec/support/domain.tld.curl
>
> Then using fakeweb you can do:
>
> FakeWeb.register_uri(@url, :file => File.join(SPEC_DIR, 'support',
> 'domain.tld.curl'))

There's one other way, right now: you can craft a custom
Net::HTTPResponse object:

fake_response = Net::HTTPOK.new('1.1', '200', 'OK')
fake_response['Content-Type'] = 'application/json'
FakeWeb.register_uri(:get, 'http://example.com', :response =>
fake_response)
response = Net::HTTP.start('example.com') { |query| query.get('/') }
puts response['Content-Type'] # "application/json"

> Ideally I'm looking to do something like:
>
> FakeWeb.register_uri(@url, :file => File.join(SPEC_DIR, 'support',
> 'domain.tld.curl'), :content_encoding => "gzip")

Sounds good to me!

http://github.com/chrisk/fakeweb/commit/b8fbab47133e3c9089e9218e13a268b9232e1d21

Chris

Ben Schwarz

unread,
Jun 24, 2009, 9:58:27 AM6/24/09
to FakeWeb
Hey,

Have you tested this implementation with OpenURI? I'm getting an empty
content-encoding array?

Cheers,

Ben
> http://github.com/chrisk/fakeweb/commit/b8fbab47133e3c9089e9218e13a26...
>
> Chris

Ben Schwarz

unread,
Jun 24, 2009, 10:34:53 AM6/24/09
to FakeWeb
I found that when I used the :response option and actually had a
recorded response - I got what I was after! :)
Reply all
Reply to author
Forward
0 new messages