pebblebed 0.0.11 → 0.0.12

Sign up to get free protection for your applications and to get access to all the features.
@@ -54,18 +54,20 @@ module Pebblebed
54
54
  end
55
55
 
56
56
  def self.post(url, params, &block)
57
- url, params = url_and_params_from_args(url, params, &block)
58
- handle_curl_response(Curl::Easy.http_post(url.to_s, params.to_json) do |curl|
57
+ url, params = url_and_params_from_args(url, params, &block)
58
+ body = params.is_a?(String) ? params : params.to_json
59
+ handle_curl_response(Curl::Easy.http_post(url.to_s, body) do |curl|
59
60
  curl.headers['Accept'] = 'application/json'
60
- curl.headers['Content-Type'] = 'application/json'
61
+ curl.headers['Content-Type'] = params.is_a?(String) ? 'text/plain' : 'application/json'
61
62
  end)
62
63
  end
63
64
 
64
65
  def self.put(url, params, &block)
65
66
  url, params = url_and_params_from_args(url, params, &block)
66
- handle_curl_response(Curl::Easy.http_put(url.to_s, params.to_json) do |curl|
67
+ body = params.is_a?(String) ? params : params.to_json
68
+ handle_curl_response(Curl::Easy.http_put(url.to_s, body) do |curl|
67
69
  curl.headers['Accept'] = 'application/json'
68
- curl.headers['Content-Type'] = 'application/json'
70
+ curl.headers['Content-Type'] = params.is_a?(String) ? 'text/plain' : 'application/json'
69
71
  end)
70
72
  end
71
73
 
@@ -1,3 +1,3 @@
1
1
  module Pebblebed
2
- VERSION = "0.0.11"
2
+ VERSION = "0.0.12"
3
3
  end
data/spec/http_spec.rb CHANGED
@@ -1,6 +1,24 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Pebblebed::Http do
4
+
5
+ let :mock_pebble do
6
+ MockPebble.new
7
+ end
8
+
9
+ let :pebble_url do
10
+ "http://localhost:8666/api/mock/v1/echo"
11
+ end
12
+
13
+ before :all do
14
+ # Starts the mock pebble at localhost:8666/api/mock/v1
15
+ mock_pebble.start
16
+ end
17
+
18
+ after :all do
19
+ mock_pebble.shutdown
20
+ end
21
+
4
22
  it "knows how to pack params into a http query string" do
5
23
  Pebblebed::Http.send(:url_with_params, URI("/dingo/"), {a:1}).should eq "/dingo/?a=1"
6
24
  end
@@ -25,4 +43,28 @@ describe Pebblebed::Http do
25
43
  -> { Pebblebed::Http.send(:handle_http_errors, DeepStruct.wrap(status:400, url:"/foobar", body:"Oh noes")) }.should raise_error Pebblebed::HttpError
26
44
  end
27
45
 
46
+ it "encodes posts and puts as json if the params is a hash" do
47
+ ['post', 'put'].each do |method|
48
+ response = Pebblebed::Http.send(method.to_sym, pebble_url, {hello:'world'})
49
+ result = JSON.parse(response.body)
50
+ result["CONTENT_TYPE"].should eq "application/json"
51
+ JSON.parse(result["BODY"])['hello'].should eq 'world'
52
+ end
53
+ end
54
+
55
+ it "encodes posts and puts as text/plain if param is string" do
56
+ ['post', 'put'].each do |method|
57
+ response = Pebblebed::Http.send(method.to_sym, pebble_url, "Hello world")
58
+ result = JSON.parse(response.body)
59
+ result["CONTENT_TYPE"].should eq "text/plain"
60
+ result["BODY"].should eq "Hello world"
61
+ end
62
+ end
63
+
64
+ it "encodes gets as url params" do
65
+ response = Pebblebed::Http.get(pebble_url, {hello: 'world'})
66
+ result = JSON.parse(response.body)
67
+ result["QUERY_STRING"].should eq "hello=world"
68
+ end
69
+
28
70
  end
@@ -0,0 +1,60 @@
1
+ # A simple echo service that emulates a pebble for the purpose of
2
+ # testing http interactions. The mock pebble is mounted at
3
+ # http://localhost:8666/api/mock/v1
4
+
5
+ require 'webrick'
6
+ require 'json'
7
+
8
+ class MockPebble
9
+ class Servlet < WEBrick::HTTPServlet::AbstractServlet
10
+
11
+ def do_GET(request, response)
12
+ status, content_type, body = do_stuff_with(request)
13
+
14
+ response.status = status
15
+ response['Content-Type'] = content_type
16
+ response.body = body
17
+ end
18
+
19
+ def do_POST(request, response)
20
+ status, content_type, body = do_stuff_with(request)
21
+
22
+ response.status = status
23
+ response['Content-Type'] = content_type
24
+ response.body = body
25
+ end
26
+
27
+ def do_PUT(request, response)
28
+ status, content_type, body = do_stuff_with(request)
29
+
30
+ response.status = status
31
+ response['Content-Type'] = content_type
32
+ response.body = body
33
+ end
34
+
35
+ def do_DELETE(request, response)
36
+ status, content_type, body = do_stuff_with(request)
37
+
38
+ response.status = status
39
+ response['Content-Type'] = content_type
40
+ response.body = body
41
+ end
42
+
43
+ def do_stuff_with(request)
44
+ return 200, "application/json", request.meta_vars.merge("BODY" => request.body).to_json
45
+ end
46
+ end
47
+
48
+ def start
49
+ @server = WEBrick::HTTPServer.new(:Port => 8666, :AccessLog => [])
50
+ @server.mount "/api/mock/v1", Servlet
51
+ @server_thread = Thread.new do
52
+ @server.start
53
+ end
54
+ end
55
+
56
+ def shutdown
57
+ @server_thread.kill
58
+ end
59
+
60
+ end
data/spec/spec_helper.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  require 'simplecov'
2
2
  require './spec/mockcached'
3
+ require './spec/mock_pebble'
3
4
  require 'bundler'
4
5
  require 'rspec'
5
6
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pebblebed
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.11
4
+ version: 0.0.12
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -14,7 +14,7 @@ date: 2012-03-08 00:00:00.000000000Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rspec
17
- requirement: &70169859432760 !ruby/object:Gem::Requirement
17
+ requirement: &70251259507760 !ruby/object:Gem::Requirement
18
18
  none: false
19
19
  requirements:
20
20
  - - ! '>='
@@ -22,10 +22,10 @@ dependencies:
22
22
  version: '0'
23
23
  type: :development
24
24
  prerelease: false
25
- version_requirements: *70169859432760
25
+ version_requirements: *70251259507760
26
26
  - !ruby/object:Gem::Dependency
27
27
  name: rake
28
- requirement: &70169859432160 !ruby/object:Gem::Requirement
28
+ requirement: &70251259507040 !ruby/object:Gem::Requirement
29
29
  none: false
30
30
  requirements:
31
31
  - - ! '>='
@@ -33,10 +33,10 @@ dependencies:
33
33
  version: '0'
34
34
  type: :development
35
35
  prerelease: false
36
- version_requirements: *70169859432160
36
+ version_requirements: *70251259507040
37
37
  - !ruby/object:Gem::Dependency
38
38
  name: simplecov
39
- requirement: &70169859431360 !ruby/object:Gem::Requirement
39
+ requirement: &70251259506620 !ruby/object:Gem::Requirement
40
40
  none: false
41
41
  requirements:
42
42
  - - ! '>='
@@ -44,10 +44,10 @@ dependencies:
44
44
  version: '0'
45
45
  type: :development
46
46
  prerelease: false
47
- version_requirements: *70169859431360
47
+ version_requirements: *70251259506620
48
48
  - !ruby/object:Gem::Dependency
49
49
  name: deepstruct
50
- requirement: &70169859430280 !ruby/object:Gem::Requirement
50
+ requirement: &70251259506200 !ruby/object:Gem::Requirement
51
51
  none: false
52
52
  requirements:
53
53
  - - ! '>='
@@ -55,10 +55,10 @@ dependencies:
55
55
  version: '0'
56
56
  type: :runtime
57
57
  prerelease: false
58
- version_requirements: *70169859430280
58
+ version_requirements: *70251259506200
59
59
  - !ruby/object:Gem::Dependency
60
60
  name: curb
61
- requirement: &70169859429020 !ruby/object:Gem::Requirement
61
+ requirement: &70251259505620 !ruby/object:Gem::Requirement
62
62
  none: false
63
63
  requirements:
64
64
  - - ~>
@@ -66,10 +66,10 @@ dependencies:
66
66
  version: 0.8.0
67
67
  type: :runtime
68
68
  prerelease: false
69
- version_requirements: *70169859429020
69
+ version_requirements: *70251259505620
70
70
  - !ruby/object:Gem::Dependency
71
71
  name: yajl-ruby
72
- requirement: &70169859427740 !ruby/object:Gem::Requirement
72
+ requirement: &70251259505140 !ruby/object:Gem::Requirement
73
73
  none: false
74
74
  requirements:
75
75
  - - ! '>='
@@ -77,10 +77,10 @@ dependencies:
77
77
  version: '0'
78
78
  type: :runtime
79
79
  prerelease: false
80
- version_requirements: *70169859427740
80
+ version_requirements: *70251259505140
81
81
  - !ruby/object:Gem::Dependency
82
82
  name: queryparams
83
- requirement: &70169859427260 !ruby/object:Gem::Requirement
83
+ requirement: &70251259504540 !ruby/object:Gem::Requirement
84
84
  none: false
85
85
  requirements:
86
86
  - - ! '>='
@@ -88,10 +88,10 @@ dependencies:
88
88
  version: '0'
89
89
  type: :runtime
90
90
  prerelease: false
91
- version_requirements: *70169859427260
91
+ version_requirements: *70251259504540
92
92
  - !ruby/object:Gem::Dependency
93
93
  name: futurevalue
94
- requirement: &70169859426620 !ruby/object:Gem::Requirement
94
+ requirement: &70251259497380 !ruby/object:Gem::Requirement
95
95
  none: false
96
96
  requirements:
97
97
  - - ! '>='
@@ -99,10 +99,10 @@ dependencies:
99
99
  version: '0'
100
100
  type: :runtime
101
101
  prerelease: false
102
- version_requirements: *70169859426620
102
+ version_requirements: *70251259497380
103
103
  - !ruby/object:Gem::Dependency
104
104
  name: pathbuilder
105
- requirement: &70169859425580 !ruby/object:Gem::Requirement
105
+ requirement: &70251259495900 !ruby/object:Gem::Requirement
106
106
  none: false
107
107
  requirements:
108
108
  - - ! '>='
@@ -110,10 +110,10 @@ dependencies:
110
110
  version: '0'
111
111
  type: :runtime
112
112
  prerelease: false
113
- version_requirements: *70169859425580
113
+ version_requirements: *70251259495900
114
114
  - !ruby/object:Gem::Dependency
115
115
  name: nokogiri
116
- requirement: &70169859415540 !ruby/object:Gem::Requirement
116
+ requirement: &70251259495400 !ruby/object:Gem::Requirement
117
117
  none: false
118
118
  requirements:
119
119
  - - ! '>='
@@ -121,10 +121,10 @@ dependencies:
121
121
  version: '0'
122
122
  type: :runtime
123
123
  prerelease: false
124
- version_requirements: *70169859415540
124
+ version_requirements: *70251259495400
125
125
  - !ruby/object:Gem::Dependency
126
126
  name: i18n
127
- requirement: &70169859414820 !ruby/object:Gem::Requirement
127
+ requirement: &70251259494960 !ruby/object:Gem::Requirement
128
128
  none: false
129
129
  requirements:
130
130
  - - ! '>='
@@ -132,10 +132,10 @@ dependencies:
132
132
  version: '0'
133
133
  type: :runtime
134
134
  prerelease: false
135
- version_requirements: *70169859414820
135
+ version_requirements: *70251259494960
136
136
  - !ruby/object:Gem::Dependency
137
137
  name: activesupport
138
- requirement: &70169859412820 !ruby/object:Gem::Requirement
138
+ requirement: &70251259494220 !ruby/object:Gem::Requirement
139
139
  none: false
140
140
  requirements:
141
141
  - - ! '>='
@@ -143,7 +143,7 @@ dependencies:
143
143
  version: '0'
144
144
  type: :runtime
145
145
  prerelease: false
146
- version_requirements: *70169859412820
146
+ version_requirements: *70251259494220
147
147
  description: Development tools for working with Pebblebed
148
148
  email:
149
149
  - katrina@bengler.no
@@ -175,6 +175,7 @@ files:
175
175
  - spec/connector_spec.rb
176
176
  - spec/generic_client_spec.rb
177
177
  - spec/http_spec.rb
178
+ - spec/mock_pebble.rb
178
179
  - spec/mockcached.rb
179
180
  - spec/spec_helper.rb
180
181
  - spec/uid_spec.rb
@@ -208,6 +209,7 @@ test_files:
208
209
  - spec/connector_spec.rb
209
210
  - spec/generic_client_spec.rb
210
211
  - spec/http_spec.rb
212
+ - spec/mock_pebble.rb
211
213
  - spec/mockcached.rb
212
214
  - spec/spec_helper.rb
213
215
  - spec/uid_spec.rb