alexvollmer-httparty 0.2.6 → 0.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/History +34 -0
- data/Manifest +25 -2
- data/README +2 -2
- data/Rakefile +6 -2
- data/bin/httparty +39 -44
- data/cucumber.yml +1 -0
- data/examples/basic.rb +6 -1
- data/examples/delicious.rb +1 -0
- data/examples/rubyurl.rb +1 -1
- data/features/basic_authentication.feature +20 -0
- data/features/command_line.feature +7 -0
- data/features/deals_with_http_error_codes.feature +26 -0
- data/features/handles_multiple_formats.feature +34 -0
- data/features/steps/env.rb +15 -0
- data/features/steps/httparty_response_steps.rb +26 -0
- data/features/steps/httparty_steps.rb +15 -0
- data/features/steps/mongrel_helper.rb +55 -0
- data/features/steps/remote_service_steps.rb +47 -0
- data/features/supports_redirection.feature +22 -0
- data/httparty.gemspec +4 -7
- data/lib/core_extensions.rb +48 -222
- data/lib/httparty/cookie_hash.rb +9 -0
- data/lib/httparty/exceptions.rb +3 -0
- data/lib/httparty/module_inheritable_attributes.rb +25 -0
- data/lib/httparty/parsers/json.rb +74 -0
- data/lib/httparty/parsers/xml.rb +209 -0
- data/lib/httparty/parsers.rb +4 -0
- data/lib/httparty/request.rb +63 -76
- data/lib/httparty/response.rb +17 -0
- data/lib/httparty/version.rb +2 -2
- data/lib/httparty.rb +108 -19
- data/spec/fixtures/empty.xml +0 -0
- data/spec/fixtures/undefined_method_add_node_for_nil.xml +2 -0
- data/spec/hash_spec.rb +49 -0
- data/spec/httparty/cookie_hash_spec.rb +38 -0
- data/spec/httparty/parsers/json_spec.rb +42 -0
- data/spec/httparty/parsers/xml_spec.rb +445 -0
- data/spec/httparty/request_spec.rb +219 -80
- data/spec/httparty/response_spec.rb +53 -0
- data/spec/httparty_spec.rb +125 -64
- data/spec/spec_helper.rb +5 -8
- data/spec/string_spec.rb +27 -0
- metadata +34 -14
- data/lib/module_level_inheritable_attributes.rb +0 -25
- data/spec/as_buggery_spec.rb +0 -16
data/History
CHANGED
@@ -1,3 +1,37 @@
|
|
1
|
+
== 0.3.1 2009-02-10
|
2
|
+
* 1 minor fix, 1 minor enhancement
|
3
|
+
* Fixed unescaping umlauts (siebertm)
|
4
|
+
* Added yaml response parsing (Miha Filej)
|
5
|
+
|
6
|
+
== 0.3.0 2009-01-31
|
7
|
+
* 1 major enhancement, 1 bug fix
|
8
|
+
* JSON gem no longer a requirement. It was conflicting with rails json stuff so I just stole ActiveSupport's json decoding and bundled it with HTTParty.
|
9
|
+
* Fixed bug where query strings were being duplicated on redirects
|
10
|
+
* Added a bunch of specs and moved some code around.
|
11
|
+
|
12
|
+
== 0.2.10 2009-01-29
|
13
|
+
* 1 minor enhancement
|
14
|
+
* Made encoding on query parameters treat everything except URI::PATTERN::UNRESERVED as UNSAFE to force encoding of '+' character (Julian Russell)
|
15
|
+
|
16
|
+
== 0.2.9 2009-01-29
|
17
|
+
* 3 minor enhancements
|
18
|
+
* Added a 'headers' accessor to the response with a hash of any HTTP headers. (Don Peterson)
|
19
|
+
* Add support for a ":cookies" option to be used at the class level, or as an option on any individual call. It should be passed a hash, which will be converted to the proper format and added to the request headers when the call is made. (Don Peterson)
|
20
|
+
* Refactored several specs and added a full suite of cucumber features (Don Peterson)
|
21
|
+
|
22
|
+
== 0.2.8 2009-01-28
|
23
|
+
* 1 major fix
|
24
|
+
* fixed major bug with response where it wouldn't iterate or really work at all with parsed responses
|
25
|
+
|
26
|
+
== 0.2.7 2009-01-28
|
27
|
+
* 2 minor fixes, 2 minor enhancements, 2 major enhancements
|
28
|
+
* fixed undefined method add_node for nil class error that occasionally happened (juliocesar)
|
29
|
+
* Handle nil or unexpected values better when typecasting. (Brian Landau)
|
30
|
+
* More robust handling of mime types (Alex Vollmer)
|
31
|
+
* Fixed support for specifying headers and added support for basic auth to CLI. (Alex Vollmer)
|
32
|
+
* Added first class response object that includes original body and status code (Alex Vollmer)
|
33
|
+
* Now parsing all response types as some non-200 responses provide important information, this means no more exception raising (Alex Vollmer)
|
34
|
+
|
1
35
|
== 0.2.6 2009-01-05
|
2
36
|
* 1 minor bug fix
|
3
37
|
* added explicit require of time as Time#parse failed outside of rails (willcodeforfoo)
|
data/Manifest
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
bin/httparty
|
2
|
+
cucumber.yml
|
2
3
|
examples/aaws.rb
|
3
4
|
examples/basic.rb
|
4
5
|
examples/delicious.rb
|
@@ -6,27 +7,49 @@ examples/google.rb
|
|
6
7
|
examples/rubyurl.rb
|
7
8
|
examples/twitter.rb
|
8
9
|
examples/whoismyrep.rb
|
10
|
+
features/basic_authentication.feature
|
11
|
+
features/command_line.feature
|
12
|
+
features/deals_with_http_error_codes.feature
|
13
|
+
features/handles_multiple_formats.feature
|
14
|
+
features/steps/env.rb
|
15
|
+
features/steps/httparty_response_steps.rb
|
16
|
+
features/steps/httparty_steps.rb
|
17
|
+
features/steps/mongrel_helper.rb
|
18
|
+
features/steps/remote_service_steps.rb
|
19
|
+
features/supports_redirection.feature
|
9
20
|
History
|
10
21
|
httparty.gemspec
|
11
22
|
lib/core_extensions.rb
|
23
|
+
lib/httparty/cookie_hash.rb
|
12
24
|
lib/httparty/exceptions.rb
|
25
|
+
lib/httparty/module_inheritable_attributes.rb
|
26
|
+
lib/httparty/parsers/json.rb
|
27
|
+
lib/httparty/parsers/xml.rb
|
28
|
+
lib/httparty/parsers.rb
|
13
29
|
lib/httparty/request.rb
|
30
|
+
lib/httparty/response.rb
|
14
31
|
lib/httparty/version.rb
|
15
32
|
lib/httparty.rb
|
16
|
-
lib/module_level_inheritable_attributes.rb
|
17
33
|
Manifest
|
18
34
|
MIT-LICENSE
|
19
35
|
Rakefile
|
20
36
|
README
|
21
37
|
setup.rb
|
22
|
-
spec/as_buggery_spec.rb
|
23
38
|
spec/fixtures/delicious.xml
|
39
|
+
spec/fixtures/empty.xml
|
24
40
|
spec/fixtures/google.html
|
25
41
|
spec/fixtures/twitter.json
|
26
42
|
spec/fixtures/twitter.xml
|
43
|
+
spec/fixtures/undefined_method_add_node_for_nil.xml
|
44
|
+
spec/hash_spec.rb
|
45
|
+
spec/httparty/cookie_hash_spec.rb
|
46
|
+
spec/httparty/parsers/json_spec.rb
|
47
|
+
spec/httparty/parsers/xml_spec.rb
|
27
48
|
spec/httparty/request_spec.rb
|
49
|
+
spec/httparty/response_spec.rb
|
28
50
|
spec/httparty_spec.rb
|
29
51
|
spec/spec.opts
|
30
52
|
spec/spec_helper.rb
|
53
|
+
spec/string_spec.rb
|
31
54
|
website/css/common.css
|
32
55
|
website/index.html
|
data/README
CHANGED
@@ -25,11 +25,11 @@ grokking the structure of output). This can also be overridden to output
|
|
25
25
|
formatted XML or JSON. Execute <tt>httparty --help</tt> for all the
|
26
26
|
options. Below is an example of how easy it is.
|
27
27
|
|
28
|
-
httparty "http://twitter.com/statuses/public_timeline.json"
|
28
|
+
httparty "http://twitter.com/statuses/public_timeline.json"
|
29
29
|
|
30
30
|
== REQUIREMENTS:
|
31
31
|
|
32
|
-
*
|
32
|
+
* You like to party!
|
33
33
|
|
34
34
|
== INSTALL:
|
35
35
|
|
data/Rakefile
CHANGED
@@ -6,6 +6,7 @@ require 'rake'
|
|
6
6
|
require 'echoe'
|
7
7
|
require 'spec/rake/spectask'
|
8
8
|
require "lib/#{ProjectName}/version"
|
9
|
+
require 'cucumber/rake/task'
|
9
10
|
|
10
11
|
Echoe.new(ProjectName, HTTParty::Version) do |p|
|
11
12
|
p.description = "Makes http fun! Also, makes consuming restful web services dead easy."
|
@@ -13,7 +14,6 @@ Echoe.new(ProjectName, HTTParty::Version) do |p|
|
|
13
14
|
p.url = "http://#{ProjectName}.rubyforge.org"
|
14
15
|
p.author = "John Nunemaker"
|
15
16
|
p.email = "nunemaker@gmail.com"
|
16
|
-
p.extra_deps = [['json', '~> 1.1']]
|
17
17
|
p.need_tar_gz = false
|
18
18
|
p.docs_host = WebsitePath
|
19
19
|
end
|
@@ -40,4 +40,8 @@ Rake::Task[:default].prerequisites.clear
|
|
40
40
|
task :default => :spec
|
41
41
|
Spec::Rake::SpecTask.new do |t|
|
42
42
|
t.spec_files = FileList["spec/**/*_spec.rb"]
|
43
|
-
end
|
43
|
+
end
|
44
|
+
|
45
|
+
Cucumber::Rake::Task.new(:features) do |t|
|
46
|
+
t.cucumber_opts = "--format pretty"
|
47
|
+
end
|
data/bin/httparty
CHANGED
@@ -2,7 +2,6 @@
|
|
2
2
|
|
3
3
|
require "optparse"
|
4
4
|
require "pp"
|
5
|
-
require "rexml/document"
|
6
5
|
|
7
6
|
$:.unshift(File.join(File.dirname(__FILE__), "/../lib"))
|
8
7
|
require "httparty"
|
@@ -10,26 +9,30 @@ require "httparty"
|
|
10
9
|
opts = {
|
11
10
|
:action => :get,
|
12
11
|
:headers => {},
|
13
|
-
:
|
14
|
-
:verbose => false,
|
15
|
-
:pretty_print => false
|
12
|
+
:verbose => false
|
16
13
|
}
|
17
14
|
|
15
|
+
def die(msg)
|
16
|
+
STDERR.puts(msg)
|
17
|
+
exit 1
|
18
|
+
end
|
19
|
+
|
18
20
|
OptionParser.new do |o|
|
19
21
|
o.banner = "USAGE: #{$0} [options] [url]"
|
22
|
+
|
20
23
|
o.on("-f",
|
21
24
|
"--format [FORMAT]",
|
22
|
-
"
|
23
|
-
|
24
|
-
|
25
|
-
o.on("-r", "--ruby", "Dump output in Ruby pretty-print format") do |r|
|
26
|
-
opts[:pretty_print] = true
|
25
|
+
"Output format to use instead of pretty-print ruby: " +
|
26
|
+
"plain, json or xml") do |f|
|
27
|
+
opts[:output_format] = f.downcase.to_sym
|
27
28
|
end
|
29
|
+
|
28
30
|
o.on("-a",
|
29
31
|
"--action [ACTION]",
|
30
32
|
"HTTP action: get (default), post, put or delete") do |a|
|
31
33
|
opts[:action] = a.downcase.to_sym
|
32
34
|
end
|
35
|
+
|
33
36
|
o.on("-d",
|
34
37
|
"--data [BODY]",
|
35
38
|
"Data to put in request body (prefix with '@' for file)") do |d|
|
@@ -39,13 +42,23 @@ OptionParser.new do |o|
|
|
39
42
|
opts[:data] = d
|
40
43
|
end
|
41
44
|
end
|
45
|
+
|
42
46
|
o.on("-H", "--header [NAME=VALUE]", "Additional HTTP headers in NAME=VALUE form") do |h|
|
43
|
-
|
44
|
-
|
47
|
+
die "Invalid header specification, should be Name:Value" unless h =~ /.+:.+/
|
48
|
+
name, value = h.split(':')
|
49
|
+
opts[:headers][name.strip] = value.strip
|
45
50
|
end
|
51
|
+
|
46
52
|
o.on("-v", "--verbose", "If set, print verbose output") do |v|
|
47
53
|
opts[:verbose] = true
|
48
54
|
end
|
55
|
+
|
56
|
+
o.on("-u", "--user [CREDS]", "Use basic authentication. Value should be user:password") do |u|
|
57
|
+
die "Invalid credentials format. Must be user:password" unless u =~ /.+:.+/
|
58
|
+
user, password = u.split(':')
|
59
|
+
opts[:basic_auth] = { :username => user, :password => password }
|
60
|
+
end
|
61
|
+
|
49
62
|
o.on("-h", "--help", "Show help documentation") do |h|
|
50
63
|
puts o
|
51
64
|
exit
|
@@ -59,45 +72,27 @@ if ARGV.empty?
|
|
59
72
|
STDERR.puts "USAGE: #{$0} [options] [url]"
|
60
73
|
end
|
61
74
|
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
if xml_decl.encoding != "UTF-8" && !output.kind_of?(Output)
|
67
|
-
output = Output.new( output, xml_decl.encoding )
|
68
|
-
end
|
69
|
-
formatter = if indent > -1
|
70
|
-
if transitive
|
71
|
-
REXML::Formatters::Transitive.new( indent, ie_hack )
|
72
|
-
else
|
73
|
-
REXML::Formatters::Pretty.new( indent, ie_hack )
|
74
|
-
end
|
75
|
-
else
|
76
|
-
REXML::Formatters::Default.new( ie_hack )
|
77
|
-
end
|
78
|
-
formatter.write( self, output )
|
79
|
-
end
|
80
|
-
end
|
81
|
-
end
|
82
|
-
|
83
|
-
if opts[:pretty_print] || opts[:format].nil?
|
84
|
-
pp HTTParty.send(opts[:action], ARGV.first, opts)
|
75
|
+
if opts[:output_format].nil?
|
76
|
+
response = HTTParty.send(opts[:action], ARGV.first, opts)
|
77
|
+
puts "Status: #{response.code}"
|
78
|
+
pp response
|
85
79
|
else
|
86
|
-
print_format = opts[:
|
87
|
-
opts.merge!(:format => :plain) if opts[:format]
|
80
|
+
print_format = opts[:output_format]
|
88
81
|
response = HTTParty.send(opts[:action], ARGV.first, opts)
|
89
|
-
|
90
|
-
|
91
|
-
pp response
|
92
|
-
else
|
93
|
-
case print_format
|
82
|
+
puts "Status: #{response.code}"
|
83
|
+
case opts[:output_format]
|
94
84
|
when :json
|
95
|
-
|
85
|
+
begin
|
86
|
+
require 'rubygems'
|
87
|
+
require 'json'
|
88
|
+
puts JSON.pretty_generate(response.delegate)
|
89
|
+
rescue LoadError
|
90
|
+
puts YAML.dump(response.delegate)
|
91
|
+
end
|
96
92
|
when :xml
|
97
|
-
REXML::Document.new(response.
|
93
|
+
REXML::Document.new(response.body).write(STDOUT, 2)
|
98
94
|
puts
|
99
95
|
else
|
100
96
|
puts response
|
101
|
-
end
|
102
97
|
end
|
103
98
|
end
|
data/cucumber.yml
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
default: features
|
data/examples/basic.rb
CHANGED
@@ -3,4 +3,9 @@ require File.join(dir, 'httparty')
|
|
3
3
|
require 'pp'
|
4
4
|
|
5
5
|
# You can also use post, put, delete in the same fashion
|
6
|
-
|
6
|
+
response = HTTParty.get('http://twitter.com/statuses/public_timeline.json')
|
7
|
+
puts response.body, response.code, response.headers.inspect
|
8
|
+
|
9
|
+
response.each do |item|
|
10
|
+
puts item['user']['screen_name']
|
11
|
+
end
|
data/examples/delicious.rb
CHANGED
data/examples/rubyurl.rb
CHANGED
@@ -0,0 +1,20 @@
|
|
1
|
+
Feature: Basic Authentication
|
2
|
+
|
3
|
+
As a developer
|
4
|
+
I want to be able to use a service that requires Basic Authentication
|
5
|
+
Because that is not an uncommon requirement
|
6
|
+
|
7
|
+
Scenario: Passing no credentials to a page requiring Basic Authentication
|
8
|
+
Given a restricted page at '/protected.html'
|
9
|
+
When I call HTTParty#get with '/protected.html'
|
10
|
+
Then it should return a response with a 401 response code
|
11
|
+
|
12
|
+
Scenario: Passing proper credentials to a page requiring Basic Authentication
|
13
|
+
Given a remote service that returns 'Authenticated Page'
|
14
|
+
And that service is accessed at the path '/protected.html'
|
15
|
+
And that service is protected by Basic Authentication
|
16
|
+
And that service requires the username 'jcash' with the password 'maninblack'
|
17
|
+
When I call HTTParty#get with '/protected.html' and a basic_auth hash:
|
18
|
+
| username | password |
|
19
|
+
| jcash | maninblack |
|
20
|
+
Then the return value should match 'Authenticated Page'
|
@@ -0,0 +1,26 @@
|
|
1
|
+
Feature: Deals with HTTP error codes
|
2
|
+
|
3
|
+
As a developer
|
4
|
+
I want to be informed of non-successful responses
|
5
|
+
Because sometimes thing explode
|
6
|
+
And I should probably know what happened
|
7
|
+
|
8
|
+
Scenario: A response of '404 - Not Found'
|
9
|
+
Given a remote service that returns a 404 status code
|
10
|
+
And that service is accessed at the path '/service.html'
|
11
|
+
When I call HTTParty#get with '/service.html'
|
12
|
+
Then it should return a response with a 404 response code
|
13
|
+
|
14
|
+
Scenario: A response of '500 - Internal Server Error'
|
15
|
+
Given a remote service that returns a 500 status code
|
16
|
+
And that service is accessed at the path '/service.html'
|
17
|
+
When I call HTTParty#get with '/service.html'
|
18
|
+
Then it should return a response with a 500 response code
|
19
|
+
|
20
|
+
Scenario: A non-successful response where I need the body
|
21
|
+
Given a remote service that returns a 400 status code
|
22
|
+
And the response from the service has a body of 'Bad response'
|
23
|
+
And that service is accessed at the path '/service.html'
|
24
|
+
When I call HTTParty#get with '/service.html'
|
25
|
+
Then it should return a response with a 400 response code
|
26
|
+
And the return value should match 'Bad response'
|
@@ -0,0 +1,34 @@
|
|
1
|
+
Feature: Handles Multiple Formats
|
2
|
+
|
3
|
+
As a developer
|
4
|
+
I want to be able to consume remote services of many different formats
|
5
|
+
And I want those formats to be automatically detected and handled
|
6
|
+
Because web services take many forms
|
7
|
+
And I don't want to have to do any extra work
|
8
|
+
|
9
|
+
Scenario: An HTML service
|
10
|
+
Given a remote service that returns '<h1>Some HTML</h1>'
|
11
|
+
And that service is accessed at the path '/service.html'
|
12
|
+
And the response from the service has a Content-Type of 'text/html'
|
13
|
+
When I call HTTParty#get with '/service.html'
|
14
|
+
Then it should return a String
|
15
|
+
And the return value should match '<h1>Some HTML</h1>'
|
16
|
+
|
17
|
+
Scenario: A JSON service
|
18
|
+
Given a remote service that returns '{ "jennings": "waylon", "cash": "johnny" }'
|
19
|
+
And that service is accessed at the path '/service.json'
|
20
|
+
And the response from the service has a Content-Type of 'application/json'
|
21
|
+
When I call HTTParty#get with '/service.json'
|
22
|
+
Then it should return a Hash equaling:
|
23
|
+
| key | value |
|
24
|
+
| jennings | waylon |
|
25
|
+
| cash | johnny |
|
26
|
+
|
27
|
+
Scenario: An XML Service
|
28
|
+
Given a remote service that returns '<singer>waylon jennings</singer>'
|
29
|
+
And that service is accessed at the path '/service.xml'
|
30
|
+
And the response from the service has a Content-Type of 'text/xml'
|
31
|
+
When I call HTTParty#get with '/service.xml'
|
32
|
+
Then it should return a Hash equaling:
|
33
|
+
| key | value |
|
34
|
+
| singer | waylon jennings |
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'mongrel'
|
2
|
+
require 'activesupport'
|
3
|
+
require 'lib/httparty'
|
4
|
+
require 'spec/expectations'
|
5
|
+
|
6
|
+
Before do
|
7
|
+
port = ENV["HTTPARTY_PORT"] || 31981
|
8
|
+
@host_and_port = "0.0.0.0:#{port}"
|
9
|
+
@server = Mongrel::HttpServer.new("0.0.0.0", port)
|
10
|
+
@server.run
|
11
|
+
end
|
12
|
+
|
13
|
+
After do
|
14
|
+
@server.stop
|
15
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
Then /it should return an? (\w+)$/ do |class_string|
|
2
|
+
@response_from_httparty.should be_an_instance_of(class_string.constantize)
|
3
|
+
end
|
4
|
+
|
5
|
+
Then /the return value should match '(.*)'/ do |expected_text|
|
6
|
+
@response_from_httparty.should eql(expected_text)
|
7
|
+
end
|
8
|
+
|
9
|
+
Then /it should return a Hash equaling:/ do |hash_table|
|
10
|
+
@response_from_httparty.should be_an_instance_of(Hash)
|
11
|
+
@response_from_httparty.keys.length.should eql(hash_table.rows.length)
|
12
|
+
hash_table.hashes.each do |pair|
|
13
|
+
key, value = pair["key"], pair["value"]
|
14
|
+
@response_from_httparty.keys.should include(key)
|
15
|
+
@response_from_httparty[key].should eql(value)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
Then /it should return a response with a (\d+) response code/ do |code|
|
20
|
+
@response_from_httparty.code.should eql(code)
|
21
|
+
end
|
22
|
+
|
23
|
+
Then /it should raise an HTTParty::RedirectionTooDeep exception/ do
|
24
|
+
@exception_from_httparty.should_not be_nil
|
25
|
+
@exception_from_httparty.class.should eql(HTTParty::RedirectionTooDeep)
|
26
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
When /I call HTTParty#get with '(.*)'$/ do |url|
|
2
|
+
begin
|
3
|
+
@response_from_httparty = HTTParty.get("http://#{@host_and_port}#{url}")
|
4
|
+
rescue HTTParty::RedirectionTooDeep => e
|
5
|
+
@exception_from_httparty = e
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
When /I call HTTParty#get with '(.*)' and a basic_auth hash:/ do |url, auth_table|
|
10
|
+
h = auth_table.hashes.first
|
11
|
+
@response_from_httparty = HTTParty.get(
|
12
|
+
"http://#{@host_and_port}#{url}",
|
13
|
+
:basic_auth => { :username => h["username"], :password => h["password"] }
|
14
|
+
)
|
15
|
+
end
|
@@ -0,0 +1,55 @@
|
|
1
|
+
def basic_mongrel_handler
|
2
|
+
Class.new(Mongrel::HttpHandler) do
|
3
|
+
attr_writer :content_type, :response_body, :response_code
|
4
|
+
|
5
|
+
def initialize
|
6
|
+
@content_type = "text/html"
|
7
|
+
@response_body = ""
|
8
|
+
@response_code = 200
|
9
|
+
@custom_headers = {}
|
10
|
+
end
|
11
|
+
|
12
|
+
def process(request, response)
|
13
|
+
reply_with(response, @response_code, @response_body)
|
14
|
+
end
|
15
|
+
|
16
|
+
def reply_with(response, code, response_body)
|
17
|
+
response.start(code) do |head, body|
|
18
|
+
head["Content-Type"] = @content_type
|
19
|
+
@custom_headers.each { |k,v| head[k] = v }
|
20
|
+
body.write(response_body)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def new_mongrel_handler
|
27
|
+
basic_mongrel_handler.new
|
28
|
+
end
|
29
|
+
|
30
|
+
def add_basic_authentication_to(handler)
|
31
|
+
m = Module.new do
|
32
|
+
attr_writer :username, :password
|
33
|
+
|
34
|
+
def self.extended(base)
|
35
|
+
base.instance_eval { @custom_headers["WWW-Authenticate"] = 'Basic Realm="Super Secret Page"' }
|
36
|
+
base.class_eval { alias_method_chain :process, :basic_authentication }
|
37
|
+
end
|
38
|
+
|
39
|
+
def process_with_basic_authentication(request, response)
|
40
|
+
if authorized?(request) then process_without_basic_authentication(request, response)
|
41
|
+
else reply_with(response, 401, "Incorrect. You have 20 seconds to comply.")
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
def authorized?(request)
|
46
|
+
request.params["HTTP_AUTHORIZATION"] == "Basic " + Base64.encode64("#{@username}:#{@password}").strip
|
47
|
+
end
|
48
|
+
end
|
49
|
+
handler.extend(m)
|
50
|
+
end
|
51
|
+
|
52
|
+
def new_mongrel_redirector(target_url, relative_path = false)
|
53
|
+
target_url = "http://#{@host_and_port}#{target_url}" unless relative_path
|
54
|
+
Mongrel::RedirectHandler.new(target_url)
|
55
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
Given /a remote service that returns '(.*)'/ do |response_body|
|
2
|
+
@handler = new_mongrel_handler
|
3
|
+
Given "the response from the service has a body of '#{response_body}'"
|
4
|
+
end
|
5
|
+
|
6
|
+
Given /a remote service that returns a (\d+) status code/ do |code|
|
7
|
+
@handler = new_mongrel_handler
|
8
|
+
@handler.response_code = code
|
9
|
+
end
|
10
|
+
|
11
|
+
Given /that service is accessed at the path '(.*)'/ do |path|
|
12
|
+
@server.register(path, @handler)
|
13
|
+
end
|
14
|
+
|
15
|
+
Given /the response from the service has a Content-Type of '(.*)'/ do |content_type|
|
16
|
+
@handler.content_type = content_type
|
17
|
+
end
|
18
|
+
|
19
|
+
Given /the response from the service has a body of '(.*)'/ do |response_body|
|
20
|
+
@handler.response_body = response_body
|
21
|
+
end
|
22
|
+
|
23
|
+
Given /the url '(.*)' redirects to '(.*)'/ do |redirection_url, target_url|
|
24
|
+
@server.register redirection_url, new_mongrel_redirector(target_url)
|
25
|
+
end
|
26
|
+
|
27
|
+
Given /that service is protected by Basic Authentication/ do
|
28
|
+
add_basic_authentication_to @handler
|
29
|
+
end
|
30
|
+
|
31
|
+
Given /that service requires the username '(.*)' with the password '(.*)'/ do |username, password|
|
32
|
+
@handler.username = username
|
33
|
+
@handler.password = password
|
34
|
+
end
|
35
|
+
|
36
|
+
Given /a restricted page at '(.*)'/ do |url|
|
37
|
+
Given "a remote service that returns 'A response I will never see'"
|
38
|
+
And "that service is accessed at the path '#{url}'"
|
39
|
+
And "that service is protected by Basic Authentication"
|
40
|
+
And "that service requires the username 'something' with the password 'secret'"
|
41
|
+
end
|
42
|
+
|
43
|
+
# This joins the server thread, and halts cucumber, so you can actually hit the
|
44
|
+
# server with a browser. Runs until you kill it with Ctrl-c
|
45
|
+
Given /I want to hit this in a browser/ do
|
46
|
+
@server.acceptor.join
|
47
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
Feature: Supports Redirection
|
2
|
+
|
3
|
+
As a developer
|
4
|
+
I want to work with services that may redirect me
|
5
|
+
And I want it to follow a reasonable number of redirects
|
6
|
+
Because sometimes web services do that
|
7
|
+
|
8
|
+
Scenario: A service that redirects once
|
9
|
+
Given a remote service that returns 'Service Response'
|
10
|
+
And that service is accessed at the path '/service.html'
|
11
|
+
And the url '/redirector.html' redirects to '/service.html'
|
12
|
+
When I call HTTParty#get with '/redirector.html'
|
13
|
+
Then the return value should match 'Service Response'
|
14
|
+
|
15
|
+
# TODO: Look in to why this actually fails...
|
16
|
+
Scenario: A service that redirects to a relative URL
|
17
|
+
|
18
|
+
Scenario: A service that redirects infinitely
|
19
|
+
Given the url '/first.html' redirects to '/second.html'
|
20
|
+
And the url '/second.html' redirects to '/first.html'
|
21
|
+
When I call HTTParty#get with '/first.html'
|
22
|
+
Then it should raise an HTTParty::RedirectionTooDeep exception
|
data/httparty.gemspec
CHANGED
@@ -2,17 +2,17 @@
|
|
2
2
|
|
3
3
|
Gem::Specification.new do |s|
|
4
4
|
s.name = %q{httparty}
|
5
|
-
s.version = "0.
|
5
|
+
s.version = "0.3.1"
|
6
6
|
|
7
7
|
s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
|
8
8
|
s.authors = ["John Nunemaker"]
|
9
|
-
s.date = %q{2009-
|
9
|
+
s.date = %q{2009-02-10}
|
10
10
|
s.default_executable = %q{httparty}
|
11
11
|
s.description = %q{Makes http fun! Also, makes consuming restful web services dead easy.}
|
12
12
|
s.email = %q{nunemaker@gmail.com}
|
13
13
|
s.executables = ["httparty"]
|
14
|
-
s.extra_rdoc_files = ["bin/httparty", "lib/core_extensions.rb", "lib/httparty/exceptions.rb", "lib/httparty/request.rb", "lib/httparty/
|
15
|
-
s.files = ["bin/httparty", "examples/aaws.rb", "examples/basic.rb", "examples/delicious.rb", "examples/google.rb", "examples/rubyurl.rb", "examples/twitter.rb", "examples/whoismyrep.rb", "History", "httparty.gemspec", "lib/core_extensions.rb", "lib/httparty/exceptions.rb", "lib/httparty/request.rb", "lib/httparty/
|
14
|
+
s.extra_rdoc_files = ["bin/httparty", "lib/core_extensions.rb", "lib/httparty/cookie_hash.rb", "lib/httparty/exceptions.rb", "lib/httparty/module_inheritable_attributes.rb", "lib/httparty/parsers/json.rb", "lib/httparty/parsers/xml.rb", "lib/httparty/parsers.rb", "lib/httparty/request.rb", "lib/httparty/response.rb", "lib/httparty/version.rb", "lib/httparty.rb", "README"]
|
15
|
+
s.files = ["bin/httparty", "cucumber.yml", "examples/aaws.rb", "examples/basic.rb", "examples/delicious.rb", "examples/google.rb", "examples/rubyurl.rb", "examples/twitter.rb", "examples/whoismyrep.rb", "features/basic_authentication.feature", "features/command_line.feature", "features/deals_with_http_error_codes.feature", "features/handles_multiple_formats.feature", "features/steps/env.rb", "features/steps/httparty_response_steps.rb", "features/steps/httparty_steps.rb", "features/steps/mongrel_helper.rb", "features/steps/remote_service_steps.rb", "features/supports_redirection.feature", "History", "httparty.gemspec", "lib/core_extensions.rb", "lib/httparty/cookie_hash.rb", "lib/httparty/exceptions.rb", "lib/httparty/module_inheritable_attributes.rb", "lib/httparty/parsers/json.rb", "lib/httparty/parsers/xml.rb", "lib/httparty/parsers.rb", "lib/httparty/request.rb", "lib/httparty/response.rb", "lib/httparty/version.rb", "lib/httparty.rb", "Manifest", "MIT-LICENSE", "Rakefile", "README", "setup.rb", "spec/fixtures/delicious.xml", "spec/fixtures/empty.xml", "spec/fixtures/google.html", "spec/fixtures/twitter.json", "spec/fixtures/twitter.xml", "spec/fixtures/undefined_method_add_node_for_nil.xml", "spec/hash_spec.rb", "spec/httparty/cookie_hash_spec.rb", "spec/httparty/parsers/json_spec.rb", "spec/httparty/parsers/xml_spec.rb", "spec/httparty/request_spec.rb", "spec/httparty/response_spec.rb", "spec/httparty_spec.rb", "spec/spec.opts", "spec/spec_helper.rb", "spec/string_spec.rb", "website/css/common.css", "website/index.html"]
|
16
16
|
s.has_rdoc = true
|
17
17
|
s.homepage = %q{http://httparty.rubyforge.org}
|
18
18
|
s.post_install_message = %q{When you HTTParty, you must party hard!}
|
@@ -27,14 +27,11 @@ Gem::Specification.new do |s|
|
|
27
27
|
s.specification_version = 2
|
28
28
|
|
29
29
|
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
30
|
-
s.add_runtime_dependency(%q<json>, ["~> 1.1"])
|
31
30
|
s.add_development_dependency(%q<echoe>, [">= 0"])
|
32
31
|
else
|
33
|
-
s.add_dependency(%q<json>, ["~> 1.1"])
|
34
32
|
s.add_dependency(%q<echoe>, [">= 0"])
|
35
33
|
end
|
36
34
|
else
|
37
|
-
s.add_dependency(%q<json>, ["~> 1.1"])
|
38
35
|
s.add_dependency(%q<echoe>, [">= 0"])
|
39
36
|
end
|
40
37
|
end
|