rack-mock_json 0.0.2 → 0.0.3
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/README.md +21 -17
- data/example/rails-example/config/mock_json.yml +6 -3
- data/lib/rack/mock_json.rb +1 -0
- data/lib/rack/mock_json/element.rb +14 -0
- data/lib/rack/mock_json/middleware.rb +8 -2
- data/lib/rack/mock_json/mock.rb +5 -3
- data/lib/rack/mock_json/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3400442311feca9764c53c44ae1ce24b4b905724
|
4
|
+
data.tar.gz: 4fc30cc2f3a7938afc32ca58e931002ca193c62f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: df1807053a8e19fc4b1d8253013becb47af491e37697d797b3855d7339ba824a7f6a49a3c11997d2f46bd16941a1e386fa881da28f8074899576a24236265214
|
7
|
+
data.tar.gz: 4d4f19560c84c77acac482366ee65452c2101f7c144c6bf1587e1c4c4841638e882fc07cbfccf628337e530d72b74c1f5cf0b12a22b55e610fc9107a58214fe3
|
data/README.md
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
rack-mock_json
|
2
2
|
====================
|
3
3
|
[](http://badge.fury.io/rb/rack-mock_json)
|
4
|
-
|
4
|
+
[](https://circleci.com/gh/fukuiretu/rack-mock_json)
|
5
5
|
|
6
6
|
|
7
7
|
## Overview
|
@@ -38,30 +38,34 @@ end
|
|
38
38
|
### Config
|
39
39
|
|
40
40
|
```yaml
|
41
|
-
- request_path: 'GET /
|
42
|
-
|
41
|
+
- request_path: 'GET /users'
|
42
|
+
contents:
|
43
|
+
- '{ "name": "retu", "age" : "20" }'
|
44
|
+
- '{ "name": "jane", "age" : "25" }'
|
43
45
|
- request_path: 'POST /user'
|
44
46
|
status: 201
|
45
|
-
|
47
|
+
contents:
|
48
|
+
- '{ "name": "taro", "age" : 17 }'
|
46
49
|
```
|
47
50
|
|
48
|
-
|
49
|
-
|
50
|
-
| Propety | Required | Default |
|
51
|
-
| :------------- | :------------| :-----------|
|
52
|
-
| request_path | ○ | - |
|
53
|
-
| status | × | 200 |
|
54
|
-
| content | ○ | - |
|
55
|
-
|
51
|
+
If you have multiple elements specified in the `contents`, and then picked up at random. Also, if you specify the `mock_element_index=i` in parameter, and it returns a particular element.
|
56
52
|
|
53
|
+
Ex.
|
57
54
|
|
58
|
-
|
59
|
-
|
60
|
-
|
55
|
+
```sh
|
56
|
+
$ curl http://localhost:3000/users -X GET -d "mock_element_index=0"
|
57
|
+
{ "name": "retu", "age" : "20" }
|
58
|
+
$ curl http://localhost:3000/users -X GET -d "mock_element_index=1"
|
59
|
+
{ "name": "jane", "age" : "25" }
|
60
|
+
```
|
61
61
|
|
62
|
+
| Propety | Required | Default | Remarks |
|
63
|
+
| :------------- | :------------| :-----------| :------------------|
|
64
|
+
| request_path | ○ | - | Use the regular expression |
|
65
|
+
| status | × | 200 | HTTP Status |
|
66
|
+
| contents | ○ | - | Body Content |
|
62
67
|
|
63
|
-
|
64
|
-
- spec & CI
|
68
|
+
[Examples For more information, click here](example/rails-example/config/mock_json.yml)
|
65
69
|
|
66
70
|
|
67
71
|
|
@@ -1,5 +1,8 @@
|
|
1
|
-
- request_path: 'GET /
|
2
|
-
|
1
|
+
- request_path: 'GET /users'
|
2
|
+
contents:
|
3
|
+
- '{ "name": "retu", "age" : "20" }'
|
4
|
+
- '{ "name": "jane", "age" : "25" }'
|
3
5
|
- request_path: 'POST /user'
|
4
6
|
status: 201
|
5
|
-
|
7
|
+
contents:
|
8
|
+
- '{ "name": "taro", "age" : 17 }'
|
data/lib/rack/mock_json.rb
CHANGED
@@ -0,0 +1,14 @@
|
|
1
|
+
module Rack
|
2
|
+
module MockJson
|
3
|
+
class Element < Hashie::Dash
|
4
|
+
property :request_path, required: true
|
5
|
+
property :status, default: 200
|
6
|
+
property :contents, required: true
|
7
|
+
|
8
|
+
def pick_content(i = nil)
|
9
|
+
return contents.sample if i.nil?
|
10
|
+
contents[i]
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
@@ -8,19 +8,21 @@ module Rack
|
|
8
8
|
|
9
9
|
def call(env)
|
10
10
|
@request = Rack::Request.new(env)
|
11
|
+
|
11
12
|
mock_element = @mock.mock_element(request_path)
|
12
13
|
return @app.call(env) if mock_element.nil?
|
13
14
|
|
15
|
+
content = mock_element.pick_content(mock_element_index)
|
14
16
|
[
|
15
17
|
mock_element.status,
|
16
18
|
{
|
17
19
|
"Content-Type" => "application/json",
|
18
|
-
"Content-Length" =>
|
20
|
+
"Content-Length" => content.bytesize.to_s,
|
19
21
|
"X-XSS-Protection" => "1; mode=block",
|
20
22
|
"X-Content-Type-Options" => "nosniff",
|
21
23
|
"X-Frame-Options" => "SAMEORIGIN"
|
22
24
|
},
|
23
|
-
[
|
25
|
+
[content]
|
24
26
|
]
|
25
27
|
end
|
26
28
|
|
@@ -29,6 +31,10 @@ module Rack
|
|
29
31
|
def request_path
|
30
32
|
"#{@request.request_method} #{@request.path_info}"
|
31
33
|
end
|
34
|
+
|
35
|
+
def mock_element_index
|
36
|
+
@request.params["mock_element_index"] ? @request.params["mock_element_index"].to_i : nil
|
37
|
+
end
|
32
38
|
end
|
33
39
|
end
|
34
40
|
end
|
data/lib/rack/mock_json/mock.rb
CHANGED
@@ -9,9 +9,11 @@ module Rack
|
|
9
9
|
element = @config.find { |e| path.match(/#{e["request_path"]}/).present? }
|
10
10
|
return nil if element.blank?
|
11
11
|
|
12
|
-
|
13
|
-
|
14
|
-
|
12
|
+
Element.new(
|
13
|
+
request_path: element['request_path'],
|
14
|
+
contents: element['contents'],
|
15
|
+
status: element['status'] ? element['status'] : 200
|
16
|
+
)
|
15
17
|
end
|
16
18
|
end
|
17
19
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rack-mock_json
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- fukuiretu
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-10-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -184,6 +184,7 @@ files:
|
|
184
184
|
- example/rails-example/vendor/assets/stylesheets/.keep
|
185
185
|
- lib/rack-mock_json.rb
|
186
186
|
- lib/rack/mock_json.rb
|
187
|
+
- lib/rack/mock_json/element.rb
|
187
188
|
- lib/rack/mock_json/middleware.rb
|
188
189
|
- lib/rack/mock_json/mock.rb
|
189
190
|
- lib/rack/mock_json/version.rb
|