http_stub 0.15.3 → 0.15.4
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 +4 -4
- data/lib/http_stub.rb +2 -0
- data/lib/http_stub/configurer/request/stub_response.rb +13 -15
- data/lib/http_stub/extensions/core/hash.rb +4 -0
- data/lib/http_stub/hash_with_indifferent_and_insensitive_access.rb +18 -0
- data/lib/http_stub/version.rb +1 -1
- data/spec/lib/http_stub/configurer/request/stub_response_spec.rb +43 -8
- data/spec/lib/http_stub/configurer_integration_spec.rb +28 -6
- data/spec/lib/http_stub/extensions/core/hash_spec.rb +25 -0
- data/spec/lib/http_stub/hash_with_indifferent_and_insensitive_access_spec.rb +179 -0
- metadata +5 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: fd0deffefb3609ce28def378afa514c15bf7a0f0
|
4
|
+
data.tar.gz: 2f6ea87666f76d3a454e6b7d422268a76c4169d5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 24e93f05fe3cebf14a56239a96bf10cf31d79a94db3dcdbf4699c9eeabdf65e808ebc3361cc7e28cafafd5499da8c1f6e6173e77a6f35b64243b5e08d1503533
|
7
|
+
data.tar.gz: 51e0d3f4b7ceab10f084875e3eed84f8ec83a0035f685e4d29f43366ec649a97e23236fa2df10d5b24f7ad0bb0b9558b36c25311362f59d8bd934cd8f96a3d00
|
data/lib/http_stub.rb
CHANGED
@@ -11,7 +11,9 @@ require 'active_support/core_ext/module/aliasing'
|
|
11
11
|
require 'active_support/core_ext/module/delegation'
|
12
12
|
require 'active_support/core_ext/hash/slice'
|
13
13
|
require 'active_support/core_ext/hash/deep_merge'
|
14
|
+
require 'active_support/core_ext/hash/indifferent_access'
|
14
15
|
|
16
|
+
require_relative 'http_stub/hash_with_indifferent_and_insensitive_access'
|
15
17
|
require_relative 'http_stub/extensions/core/hash'
|
16
18
|
require_relative 'http_stub/extensions/rack/handler'
|
17
19
|
require_relative 'http_stub/server/headers'
|
@@ -5,32 +5,30 @@ module HttpStub
|
|
5
5
|
class StubResponse
|
6
6
|
|
7
7
|
def initialize(id, args)
|
8
|
-
@id
|
9
|
-
@args
|
8
|
+
@id = id
|
9
|
+
@status = args[:status] || ""
|
10
|
+
@headers = (args[:headers] || {}).with_indifferent_and_insensitive_access
|
11
|
+
@body = args[:body]
|
12
|
+
@delay_in_seconds = args[:delay_in_seconds] || ""
|
10
13
|
end
|
11
14
|
|
12
15
|
def payload
|
13
|
-
{
|
14
|
-
status: @args[:status] || "",
|
15
|
-
headers: @args[:headers] || {},
|
16
|
-
body: @args[:body],
|
17
|
-
delay_in_seconds: @args[:delay_in_seconds] || ""
|
18
|
-
}
|
16
|
+
{ status: @status, headers: @headers, body: @body, delay_in_seconds: @delay_in_seconds }
|
19
17
|
end
|
20
18
|
|
21
19
|
def file
|
22
|
-
|
23
|
-
args = { id: @id, type: @args[:headers]["content-type"] }.merge(@args[:body][:file])
|
24
|
-
HttpStub::Configurer::Request::StubResponseFile.new(args)
|
25
|
-
else
|
26
|
-
nil
|
27
|
-
end
|
20
|
+
contains_file? ? create_response_file : nil
|
28
21
|
end
|
29
22
|
|
30
23
|
private
|
31
24
|
|
32
25
|
def contains_file?
|
33
|
-
@
|
26
|
+
@body.is_a?(Hash) && @body.key?(:file)
|
27
|
+
end
|
28
|
+
|
29
|
+
def create_response_file
|
30
|
+
args = { id: @id, type: @headers["content-type"] }.merge(@body[:file])
|
31
|
+
HttpStub::Configurer::Request::StubResponseFile.new(args)
|
34
32
|
end
|
35
33
|
|
36
34
|
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module HttpStub
|
2
|
+
|
3
|
+
class HashWithIndifferentAndInsensitiveAccess < HashWithIndifferentAccess
|
4
|
+
|
5
|
+
def [](key)
|
6
|
+
self.key?(key) ? super : insensitive_find(key)
|
7
|
+
end
|
8
|
+
|
9
|
+
private
|
10
|
+
|
11
|
+
def insensitive_find(key)
|
12
|
+
entry = self.find { |entry_key, _entry_value| entry_key.to_s.downcase == key.to_s.downcase }
|
13
|
+
entry ? entry[1] : nil
|
14
|
+
end
|
15
|
+
|
16
|
+
end
|
17
|
+
|
18
|
+
end
|
data/lib/http_stub/version.rb
CHANGED
@@ -6,7 +6,7 @@ describe HttpStub::Configurer::Request::StubResponse do
|
|
6
6
|
let(:stub_response) { HttpStub::Configurer::Request::StubResponse.new(stub_fixture.id, fixture.symbolized) }
|
7
7
|
|
8
8
|
describe "#payload" do
|
9
|
-
|
9
|
+
|
10
10
|
subject { stub_response.payload }
|
11
11
|
|
12
12
|
context "when a status response argument is provided" do
|
@@ -75,12 +75,13 @@ describe HttpStub::Configurer::Request::StubResponse do
|
|
75
75
|
|
76
76
|
context "when the body contains a file hash" do
|
77
77
|
|
78
|
-
let(:
|
79
|
-
let(:
|
80
|
-
let(:
|
78
|
+
let(:content_type_key) { "content-type" }
|
79
|
+
let(:content_type_value) { "some-content-type" }
|
80
|
+
let(:file_path) { "some/file/path" }
|
81
|
+
let(:file_name) { "some_file.name" }
|
81
82
|
|
82
83
|
before(:example) do
|
83
|
-
fixture.headers = {
|
84
|
+
fixture.headers = { content_type_key => content_type_value }
|
84
85
|
fixture.body = { file: { path: file_path, name: file_name } }
|
85
86
|
end
|
86
87
|
|
@@ -102,10 +103,44 @@ describe HttpStub::Configurer::Request::StubResponse do
|
|
102
103
|
subject
|
103
104
|
end
|
104
105
|
|
105
|
-
|
106
|
-
|
106
|
+
context "when the content type header key is provided" do
|
107
|
+
|
108
|
+
context "in lower case" do
|
109
|
+
|
110
|
+
let(:content_type_key) { "content-type" }
|
111
|
+
|
112
|
+
it "creates a response file whose type is the content-type response header" do
|
113
|
+
expect_response_file_to_be_created_with(type: content_type_value)
|
114
|
+
|
115
|
+
subject
|
116
|
+
end
|
117
|
+
|
118
|
+
end
|
119
|
+
|
120
|
+
context "in upper case" do
|
121
|
+
|
122
|
+
let(:content_type_key) { "Content-Type" }
|
123
|
+
|
124
|
+
it "creates a response file whose type is the content-type response header" do
|
125
|
+
expect_response_file_to_be_created_with(type: content_type_value)
|
126
|
+
|
127
|
+
subject
|
128
|
+
end
|
129
|
+
|
130
|
+
end
|
131
|
+
|
132
|
+
end
|
133
|
+
|
134
|
+
context "when the content type header is not provided" do
|
135
|
+
|
136
|
+
let(:content_type_value) { nil }
|
137
|
+
|
138
|
+
it "creates a response file whose type is nil, which allows the server to default the value" do
|
139
|
+
expect_response_file_to_be_created_with(type: nil)
|
140
|
+
|
141
|
+
subject
|
142
|
+
end
|
107
143
|
|
108
|
-
subject
|
109
144
|
end
|
110
145
|
|
111
146
|
it "returns the created response file" do
|
@@ -665,20 +665,42 @@ describe HttpStub::Configurer, "when the server is running" do
|
|
665
665
|
|
666
666
|
end
|
667
667
|
|
668
|
-
context "and the configurer stubs
|
668
|
+
context "and the configurer stubs responses with a file body" do
|
669
669
|
|
670
670
|
let(:configurer) { HttpStub::Examples::ConfigurerWithFileResponse.new }
|
671
671
|
|
672
672
|
context "and a request that matches is made" do
|
673
673
|
|
674
|
-
|
674
|
+
context "that matches a stub with a custom content-type" do
|
675
|
+
|
676
|
+
let(:response) { HTTParty.get("#{server_uri}/stub_response_with_file") }
|
677
|
+
|
678
|
+
it "responds with the configured status code" do
|
679
|
+
expect(response.code).to eql(200)
|
680
|
+
end
|
681
|
+
|
682
|
+
it "responds with the configured content type" do
|
683
|
+
expect(response.content_type).to eql("application/pdf")
|
684
|
+
end
|
685
|
+
|
686
|
+
it "responds with the configured file" do
|
687
|
+
expect_response_to_contain_file(HttpStub::Examples::ConfigurerWithFileResponse::FILE_PATH)
|
688
|
+
end
|
675
689
|
|
676
|
-
it "responds with the configured status code" do
|
677
|
-
expect(response.code).to eql(200)
|
678
690
|
end
|
679
691
|
|
680
|
-
|
681
|
-
|
692
|
+
context "that matches a stub with no content-type" do
|
693
|
+
|
694
|
+
let(:response) { HTTParty.get("#{server_uri}/stub_response_with_file_and_no_content_type") }
|
695
|
+
|
696
|
+
it "responds with a default content type of 'application/octet-stream'" do
|
697
|
+
expect(response.content_type).to eql("application/octet-stream")
|
698
|
+
end
|
699
|
+
|
700
|
+
it "responds with the configured response" do
|
701
|
+
expect_response_to_contain_file(HttpStub::Examples::ConfigurerWithFileResponse::FILE_PATH)
|
702
|
+
end
|
703
|
+
|
682
704
|
end
|
683
705
|
|
684
706
|
end
|
@@ -154,4 +154,29 @@ describe HttpStub::Extensions::Core::Hash do
|
|
154
154
|
|
155
155
|
end
|
156
156
|
|
157
|
+
describe "#with_indifferent_and_insensitive_access" do
|
158
|
+
|
159
|
+
let(:hash) { { key: "value" } }
|
160
|
+
let(:indifferent_and_insensitive_hash) { instance_double(HttpStub::HashWithIndifferentAndInsensitiveAccess) }
|
161
|
+
|
162
|
+
subject { hash.with_indifferent_and_insensitive_access }
|
163
|
+
|
164
|
+
before(:example) do
|
165
|
+
allow(HttpStub::HashWithIndifferentAndInsensitiveAccess).to(
|
166
|
+
receive(:new).and_return(indifferent_and_insensitive_hash)
|
167
|
+
)
|
168
|
+
end
|
169
|
+
|
170
|
+
it "creates a hash with indifferent and insensitive access containing the current hash" do
|
171
|
+
expect(HttpStub::HashWithIndifferentAndInsensitiveAccess).to receive(:new).with(hash)
|
172
|
+
|
173
|
+
subject
|
174
|
+
end
|
175
|
+
|
176
|
+
it "returns the created hash" do
|
177
|
+
expect(subject).to eql(indifferent_and_insensitive_hash)
|
178
|
+
end
|
179
|
+
|
180
|
+
end
|
181
|
+
|
157
182
|
end
|
@@ -0,0 +1,179 @@
|
|
1
|
+
describe HttpStub::HashWithIndifferentAndInsensitiveAccess do
|
2
|
+
|
3
|
+
let(:enhanced_hash) { HttpStub::HashWithIndifferentAndInsensitiveAccess.new(original_hash) }
|
4
|
+
|
5
|
+
describe "#[]" do
|
6
|
+
|
7
|
+
subject { enhanced_hash[key] }
|
8
|
+
|
9
|
+
context "when the original hash contains symbol keys" do
|
10
|
+
|
11
|
+
let(:original_hash) { { key: "value", another_key: "another value" } }
|
12
|
+
|
13
|
+
context "and the provided key is a symbol" do
|
14
|
+
|
15
|
+
context "that exactly matches an original hash key" do
|
16
|
+
|
17
|
+
let(:key) { :key }
|
18
|
+
|
19
|
+
it "returns the original value" do
|
20
|
+
expect(subject).to eql("value")
|
21
|
+
end
|
22
|
+
|
23
|
+
context "and another key is also a case insensitive match" do
|
24
|
+
|
25
|
+
let(:original_hash) { { Key: "Case insensitive value", key: "value" } }
|
26
|
+
|
27
|
+
it "prefers the exact match" do
|
28
|
+
expect(subject).to eql("value")
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
34
|
+
|
35
|
+
context "that is a case insensitive match for an original hash key" do
|
36
|
+
|
37
|
+
let(:key) { :Key }
|
38
|
+
|
39
|
+
it "returns the original value" do
|
40
|
+
expect(subject).to eql("value")
|
41
|
+
end
|
42
|
+
|
43
|
+
end
|
44
|
+
|
45
|
+
context "that does not match an entry in the original hash" do
|
46
|
+
|
47
|
+
let(:key) { :does_not_match }
|
48
|
+
|
49
|
+
it "returns nil" do
|
50
|
+
expect(subject).to be(nil)
|
51
|
+
end
|
52
|
+
|
53
|
+
end
|
54
|
+
|
55
|
+
end
|
56
|
+
|
57
|
+
context "and the provided key is a string" do
|
58
|
+
|
59
|
+
context "whose symbol respresentation exactly matches an original hash key" do
|
60
|
+
|
61
|
+
let(:key) { "key" }
|
62
|
+
|
63
|
+
it "returns the original value" do
|
64
|
+
expect(subject).to eql("value")
|
65
|
+
end
|
66
|
+
|
67
|
+
end
|
68
|
+
|
69
|
+
context "whose symbol representation is a case insensitive match for an original hash key" do
|
70
|
+
|
71
|
+
let(:key) { "Key" }
|
72
|
+
|
73
|
+
it "returns the original value" do
|
74
|
+
expect(subject).to eql("value")
|
75
|
+
end
|
76
|
+
|
77
|
+
end
|
78
|
+
|
79
|
+
context "that does not match an entry in the original hash" do
|
80
|
+
|
81
|
+
let(:key) { "does not match" }
|
82
|
+
|
83
|
+
it "returns nil" do
|
84
|
+
expect(subject).to be(nil)
|
85
|
+
end
|
86
|
+
|
87
|
+
end
|
88
|
+
|
89
|
+
end
|
90
|
+
|
91
|
+
end
|
92
|
+
|
93
|
+
context "when the original hash string keys" do
|
94
|
+
|
95
|
+
let(:original_hash) { { "key" => "value", "another_key" => "another value" } }
|
96
|
+
|
97
|
+
context "and the provided key is a string" do
|
98
|
+
|
99
|
+
context "that exactly matches an original hash key" do
|
100
|
+
|
101
|
+
let(:key) { "key" }
|
102
|
+
|
103
|
+
it "returns the original value" do
|
104
|
+
expect(subject).to eql("value")
|
105
|
+
end
|
106
|
+
|
107
|
+
context "and another key is also a case insensitive match" do
|
108
|
+
|
109
|
+
let(:original_hash) { { "Key" => "Case insensitive value", "key" => "value" } }
|
110
|
+
|
111
|
+
it "prefers the exact match" do
|
112
|
+
expect(subject).to eql("value")
|
113
|
+
end
|
114
|
+
|
115
|
+
end
|
116
|
+
|
117
|
+
end
|
118
|
+
|
119
|
+
context "that is a case insensitive match for an original hash key" do
|
120
|
+
|
121
|
+
let(:key) { "Key" }
|
122
|
+
|
123
|
+
it "returns the original value" do
|
124
|
+
expect(subject).to eql("value")
|
125
|
+
end
|
126
|
+
|
127
|
+
end
|
128
|
+
|
129
|
+
context "that does not match an entry in the original hash" do
|
130
|
+
|
131
|
+
let(:key) { "does not match" }
|
132
|
+
|
133
|
+
it "returns nil" do
|
134
|
+
expect(subject).to be(nil)
|
135
|
+
end
|
136
|
+
|
137
|
+
end
|
138
|
+
|
139
|
+
end
|
140
|
+
|
141
|
+
context "and the provided key is a symbol" do
|
142
|
+
|
143
|
+
context "whose string respresentation exactly matches an original hash key" do
|
144
|
+
|
145
|
+
let(:key) { :key }
|
146
|
+
|
147
|
+
it "returns the original value" do
|
148
|
+
expect(subject).to eql("value")
|
149
|
+
end
|
150
|
+
|
151
|
+
end
|
152
|
+
|
153
|
+
context "whose string representation is a case insensitive match for an original hash key" do
|
154
|
+
|
155
|
+
let(:key) { :Key }
|
156
|
+
|
157
|
+
it "returns the original value" do
|
158
|
+
expect(subject).to eql("value")
|
159
|
+
end
|
160
|
+
|
161
|
+
end
|
162
|
+
|
163
|
+
context "that does not match an entry in the original hash" do
|
164
|
+
|
165
|
+
let(:key) { :does_not_match }
|
166
|
+
|
167
|
+
it "returns nil" do
|
168
|
+
expect(subject).to be(nil)
|
169
|
+
end
|
170
|
+
|
171
|
+
end
|
172
|
+
|
173
|
+
end
|
174
|
+
|
175
|
+
end
|
176
|
+
|
177
|
+
end
|
178
|
+
|
179
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: http_stub
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.15.
|
4
|
+
version: 0.15.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Matthew Ueckerman
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2015-05-
|
12
|
+
date: 2015-05-25 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rake
|
@@ -278,6 +278,7 @@ files:
|
|
278
278
|
- "./lib/http_stub/configurer/server/request_processor.rb"
|
279
279
|
- "./lib/http_stub/extensions/core/hash.rb"
|
280
280
|
- "./lib/http_stub/extensions/rack/handler.rb"
|
281
|
+
- "./lib/http_stub/hash_with_indifferent_and_insensitive_access.rb"
|
281
282
|
- "./lib/http_stub/rake/server_daemon_tasks.rb"
|
282
283
|
- "./lib/http_stub/rake/server_tasks.rb"
|
283
284
|
- "./lib/http_stub/rake/task_generators.rb"
|
@@ -336,6 +337,7 @@ files:
|
|
336
337
|
- "./spec/lib/http_stub/configurer_spec.rb"
|
337
338
|
- "./spec/lib/http_stub/extensions/core/hash_spec.rb"
|
338
339
|
- "./spec/lib/http_stub/extensions/rack/handler_spec.rb"
|
340
|
+
- "./spec/lib/http_stub/hash_with_indifferent_and_insensitive_access_spec.rb"
|
339
341
|
- "./spec/lib/http_stub/rake/server_daemon_tasks_smoke_spec.rb"
|
340
342
|
- "./spec/lib/http_stub/rake/server_tasks_smoke_spec.rb"
|
341
343
|
- "./spec/lib/http_stub/rake/server_tasks_spec.rb"
|
@@ -421,6 +423,7 @@ test_files:
|
|
421
423
|
- "./spec/lib/http_stub/configurer_spec.rb"
|
422
424
|
- "./spec/lib/http_stub/extensions/core/hash_spec.rb"
|
423
425
|
- "./spec/lib/http_stub/extensions/rack/handler_spec.rb"
|
426
|
+
- "./spec/lib/http_stub/hash_with_indifferent_and_insensitive_access_spec.rb"
|
424
427
|
- "./spec/lib/http_stub/rake/server_daemon_tasks_smoke_spec.rb"
|
425
428
|
- "./spec/lib/http_stub/rake/server_tasks_smoke_spec.rb"
|
426
429
|
- "./spec/lib/http_stub/rake/server_tasks_spec.rb"
|