optimus_prime 3.2.0 → 4.0.0

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 56096e8c29cc2d111db55150a046098ee6126270
4
- data.tar.gz: 135faa19207432c3472c063f31c7ff587594a4d8
3
+ metadata.gz: b21594444427e0d8f033659808f7694b71d51f08
4
+ data.tar.gz: 79b3ada0c5c81ecac3c502a3e5c5763f035009b9
5
5
  SHA512:
6
- metadata.gz: d93c30608c30f43f50886596331154f2edd1f0dd6f1c79b9bdaf4015af4217849cefb5bef87146f22d443bd4dce18cf733834aeab9cb640e6033086e23833fc6
7
- data.tar.gz: b4574a0b24c877342df9a4f6eceef35121b59a1ce0fcc7140f9a37b5d9de9748d1ad6926916a96bf2f0c8d7888cf1bbdabe2b6ab85dec72ba1eb232983236754
6
+ metadata.gz: ca99401cb0c9100ed66e20997871600d97b52258cbf3f53ef3ce0500863864e74da3cb7492011a624c90e6b48a42c6788bc4a52bf504e575100ed97c616ee145
7
+ data.tar.gz: 6609b0f517db910e86d21674de883fe94183050bcc9f9f09925dbd8ebb584edf6bc08a097b0f2bd198ebb3503a9b814f4533526c569e71f8909456f6d4b7ce8b
@@ -28,19 +28,19 @@ module OptimusPrime
28
28
  return 404
29
29
  end
30
30
 
31
+ body = parse_request(response[:content_type])
32
+
31
33
  if response[:requested_with]
32
- request.body.rewind
33
- return 404 unless eval("request.body.read.include?('#{response[:requested_with]}')")
34
+ return 404 unless body.include?(response[:requested_with])
34
35
  end
35
36
 
36
37
  sleep(response[:sleep].to_i) if response[:sleep]
37
38
 
38
39
  if response[:persisted]
39
- new_body = request.body.read
40
- @@responses[path][:body] = JSON.parse(response[:body]).merge!(JSON.parse(new_body)).to_json
40
+ @@responses[path][:body] = JSON.parse(response[:body]).merge!(body)
41
41
  end
42
42
 
43
- record_request(path)
43
+ record_request(path, body)
44
44
  content_type(response[:content_type])
45
45
  status(response[:status_code] || 201)
46
46
  return "" if response[:status_code] =~ /500|404/
@@ -55,19 +55,19 @@ module OptimusPrime
55
55
  return 404
56
56
  end
57
57
 
58
+ body = parse_request(response[:content_type])
59
+
58
60
  if response[:requested_with]
59
- request.body.rewind
60
- return 404 unless eval("request.body.read.include?('#{response[:requested_with]}')")
61
+ return 404 unless body.include?(response[:requested_with])
61
62
  end
62
63
 
63
64
  sleep(response[:sleep].to_i) if response[:sleep]
64
65
 
65
66
  if response[:persisted]
66
- new_body = request.body.read
67
- @@responses[path][:body] = JSON.parse(response[:body]).merge!(JSON.parse(new_body)).to_json
67
+ @@responses[path][:body] = JSON.parse(response[:body]).merge!(body).to_json
68
68
  end
69
69
 
70
- record_request(path)
70
+ record_request(path, body)
71
71
  content_type(response[:content_type])
72
72
  status(response[:status_code] || 201)
73
73
  return "" if response[:status_code] =~ /500|404/
@@ -82,41 +82,24 @@ module OptimusPrime
82
82
  return 404
83
83
  end
84
84
 
85
+ body = parse_request(response[:content_type])
86
+
85
87
  if response[:requested_with]
86
- request.body.rewind
87
- return 404 unless eval("request.body.read.include?('#{response[:requested_with]}')")
88
+ return 404 unless body.include?(response[:requested_with])
88
89
  end
89
90
 
90
-
91
91
  if response[:persisted]
92
- new_body = params.tap { |p| p.delete("splat"); p.delete("captures") }
93
- @@responses[path][:body] = new_body.to_json
92
+ @@responses[path][:body] = body
94
93
  end
95
94
 
96
- record_request(path)
97
-
95
+ record_request(path, body)
98
96
  content_type(response[:content_type])
99
97
  status(response[:status_code])
100
-
101
98
  sleep(response[:sleep].to_i) if response[:sleep]
102
-
103
99
  return "" if response[:status_code] =~ /500|404/
104
100
  response[:body]
105
101
  end
106
102
 
107
- def record_request(path)
108
- requests[path][:count] += 1
109
- params.delete("splat")
110
- params.delete("captures")
111
- request_made = { method: self.env["REQUEST_METHOD"], body: params, headers: { content_type: request.content_type, accept: request.accept } }
112
- requests[path][:last_request] = request_made
113
- end
114
-
115
- def get_path
116
- # self.env["REQUEST_URI"].scan(/^\/get\/([\/\w+]+)(\/|\?|$)/).flatten[0]
117
- self.env["REQUEST_URI"].sub(/\/get\/|\/requests\//, "")
118
- end
119
-
120
103
  get "/get/*" do
121
104
  path = get_path
122
105
  response = responses[path]
@@ -126,7 +109,7 @@ module OptimusPrime
126
109
  return 404
127
110
  end
128
111
 
129
- record_request(path)
112
+ record_request(path, {})
130
113
 
131
114
  sleep(response[:sleep].to_f) if response[:sleep]
132
115
 
@@ -166,20 +149,42 @@ module OptimusPrime
166
149
  @@not_primed.to_json
167
150
  end
168
151
 
152
+ def record_request(path, body)
153
+ requests[path][:count] += 1
154
+ request_made = { method: self.env["REQUEST_METHOD"], body: body, headers: { content_type: request.content_type, accept: request.accept } }
155
+ @@requests[path][:last_request] = request_made
156
+ end
157
+
158
+ def get_path
159
+ # self.env["REQUEST_URI"].scan(/^\/get\/([\/\w+]+)(\/|\?|$)/).flatten[0]
160
+ self.env["REQUEST_URI"].sub(/\/get\/|\/requests\//, "")
161
+ end
162
+
163
+
169
164
  private
170
165
 
171
- def responses
172
- @@responses
166
+ def parse_request(content_type)
167
+ if content_type.match(/json/)
168
+ request_body = request.body.read
169
+ request_body = request_body.empty? ? "{}" : request_body
170
+ request_body = JSON.parse(request_body)
171
+ else
172
+ request_body = request.body.read
173
173
  end
174
+ request.body.rewind
175
+ request_body
176
+ end
174
177
 
175
- def requests
176
- @@requests
177
- end
178
+ def responses
179
+ @@responses
180
+ end
178
181
 
179
- def get_boolean(boolean)
180
- boolean == "true"
181
- end
182
+ def requests
183
+ @@requests
184
+ end
182
185
 
186
+ def get_boolean(boolean)
187
+ boolean == "true"
188
+ end
183
189
  end
184
-
185
190
  end
@@ -1,3 +1,3 @@
1
1
  module OptimusPrime
2
- VERSION = "3.2.0"
2
+ VERSION = "4.0.0"
3
3
  end
@@ -2,6 +2,12 @@ OptimusPrime::Cannon.fire!(7003)
2
2
 
3
3
  describe OptimusPrime do
4
4
 
5
+ after(:all) do
6
+ if File.exists?(File.expand_path("../../tmp/pids/optimus_prime.pid", __dir__))
7
+ system("thin stop -P #{File.expand_path("../../tmp/pids/optimus_prime.pid", __dir__)}")
8
+ end
9
+ end
10
+
5
11
  let(:op) { OptimusPrime::Base.new }
6
12
 
7
13
  it "primes an endpoint" do
@@ -9,7 +15,7 @@ describe OptimusPrime do
9
15
  end
10
16
 
11
17
  it "primes an endpoint with attributes" do
12
- op.prime("test", "I am a response")
18
+ op.prime("test", "I am a response", content_type: "text/html")
13
19
 
14
20
  response = ::Faraday.get("http://localhost:7003/get/test")
15
21
  expect( response.body ).to eq "I am a response"
@@ -68,7 +74,7 @@ describe OptimusPrime do
68
74
  context "Asserting on request content" do
69
75
 
70
76
  it "returns a 404 if the request body does not match the assertion" do
71
- op.prime("user", { username: "Test" }.to_json, content_type: :json, requested_with: "haha")
77
+ op.prime("user", { username: "Test" }.to_json, content_type: "text/html", requested_with: "haha")
72
78
 
73
79
  response = ::Faraday.post('http://localhost:7003/get/user', "I am a body")
74
80
 
@@ -76,7 +82,7 @@ describe OptimusPrime do
76
82
  end
77
83
 
78
84
  it "returns a 200 if the request body does match the assertion" do
79
- op.prime("user", { username: "Test" }.to_json, content_type: :json, requested_with: "I am a body")
85
+ op.prime("user", { username: "Test" }.to_json, content_type: :html, requested_with: "I am a body")
80
86
 
81
87
  response = ::Faraday.post('http://localhost:7003/get/user', "I am a body")
82
88
 
@@ -199,19 +205,19 @@ describe OptimusPrime do
199
205
 
200
206
  it "POST" do
201
207
  op.prime("kermit", { username: "Test" }.to_json, content_type: :json)
202
- ::Faraday.post("http://localhost:7003/get/kermit", { username: "Test" })
208
+ ::Faraday.post("http://localhost:7003/get/kermit", { username: "Test" }.to_json)
203
209
  expect( op.last_request_for("kermit") ).to eq({ "method" => "POST", "body" => { "username" => "Test" }, "headers"=>{ "content_type"=>"application/x-www-form-urlencoded", "accept"=>["*/*"]} })
204
210
  end
205
211
 
206
212
  it "PUT" do
207
213
  op.prime("put/kermit", { username: "Test" }.to_json, content_type: :json)
208
- ::Faraday.put("http://localhost:7003/get/put/kermit", { username: "Test" })
214
+ ::Faraday.put("http://localhost:7003/get/put/kermit", { username: "Test" }.to_json)
209
215
  expect( op.last_request_for("put/kermit") ).to eq({"method"=>"PUT", "body"=>{"username"=>"Test"}, "headers"=>{"content_type"=>"application/x-www-form-urlencoded", "accept"=>["*/*"]}})
210
216
  end
211
217
 
212
218
  it "returns a decoded body" do
213
219
  op.prime("kermit", { username: "Test" }.to_json, content_type: :json)
214
- ::Faraday.post("http://localhost:7003/get/kermit", { word: "with spaces and other shit" })
220
+ ::Faraday.post("http://localhost:7003/get/kermit", { word: "with spaces and other shit" }.to_json)
215
221
  expect( op.last_request_for("kermit") ).to eq({"method"=>"POST", "body"=>{"word"=>"with spaces and other shit"}, "headers"=>{"content_type"=>"application/x-www-form-urlencoded", "accept"=>["*/*"]}})
216
222
  end
217
223
 
@@ -242,8 +248,8 @@ describe OptimusPrime do
242
248
  op = OptimusPrime::Base.new(wait_for: 1)
243
249
  op.prime("expectation", { status: "UNKOWN" }, content_type: :json)
244
250
 
245
- ::Faraday.post("http://localhost:7003/get/expectation", { status: "IN_PROGRESS" } )
246
- Thread.new { sleep(0.5); ::Faraday.post("http://localhost:7003/get/expectation", { status: "COMPLETED" } )}
251
+ ::Faraday.post("http://localhost:7003/get/expectation", { status: "IN_PROGRESS" }.to_json )
252
+ Thread.new { sleep(0.5); ::Faraday.post("http://localhost:7003/get/expectation", { status: "COMPLETED" }.to_json )}
247
253
 
248
254
  op.wait_until_request("expectation") do |request|
249
255
  expect(request["body"]["status"]).to eq("COMPLETED")
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: optimus_prime
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.2.0
4
+ version: 4.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Antonio Nalesso
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-10-06 00:00:00.000000000 Z
11
+ date: 2014-11-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler