cameleon 0.1.0 → 0.1.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.
- checksums.yaml +15 -0
- data/Gemfile +3 -3
- data/README.md +43 -11
- data/Rakefile +0 -19
- data/VERSION +1 -1
- data/cameleon.gemspec +1 -1
- data/lib/cameleon/action.rb +38 -0
- data/template/gallery/response/gallery/json/_switch.rb +4 -0
- data/template/new/cameleon.yml +5 -1
- metadata +58 -36
checksums.yaml
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
---
|
2
|
+
!binary "U0hBMQ==":
|
3
|
+
metadata.gz: !binary |-
|
4
|
+
Yjg2ZDYwYjQ4NjYwNzNlOTczM2FhZjc3ZjI5NTYwNGFhMDMxMmE3OQ==
|
5
|
+
data.tar.gz: !binary |-
|
6
|
+
MDRkNmM1ZDJhMjMzZDg5MGRhY2NiMjIxYzE1YTBmODM0ZDYzOTMxMg==
|
7
|
+
!binary "U0hBNTEy":
|
8
|
+
metadata.gz: !binary |-
|
9
|
+
NzkzNzkzYzJiMWRlYTdjNTViYTFjN2U4NWU3Yjk5MzdlODhkZDhiOWI5ZDAz
|
10
|
+
MTkwYjQxNjg1NTdjOGEyNzBiZTZkMjdiNTZlOWRmYjI1OTAyMTFkNzRiZDAw
|
11
|
+
NmZjYjYwMzRiNDgwNDNiNDI3NWI2ODRiNWRjMjA1M2IzMzM2MWQ=
|
12
|
+
data.tar.gz: !binary |-
|
13
|
+
YzdhMTYyNzgwYTY0NzgzZDVmNDk5NTg4NWE2MDNjYzU3NjU0ODMwY2RjNTJj
|
14
|
+
N2I2MDNkZTMyZmUxNTZlYzgyZmFkM2RlNWE2MjU2M2Q5ZTQxNDRkZDRhZjYw
|
15
|
+
YzFlY2U0MTU3YzUzMTgwZjYyNzE2Y2NhMGJkYWFiYjMzZmFjNWI=
|
data/Gemfile
CHANGED
@@ -3,11 +3,11 @@ gem "erubis"
|
|
3
3
|
gem "rack"
|
4
4
|
gem "hashie"
|
5
5
|
gem "json"
|
6
|
+
gem "json-schema"
|
6
7
|
gem "nokogiri"
|
7
8
|
|
8
9
|
group :development do
|
9
10
|
gem "shoulda", ">= 0"
|
10
|
-
gem "bundler"
|
11
|
-
gem "jeweler"
|
12
|
-
gem "rcov", ">= 0"
|
11
|
+
gem "bundler"
|
12
|
+
gem "jeweler"#, "~> 1.6.4"
|
13
13
|
end
|
data/README.md
CHANGED
@@ -1,15 +1,17 @@
|
|
1
1
|
Cameleon
|
2
2
|
=======================================
|
3
3
|
|
4
|
-
Cameleon (not Chameleon!) is HTTP mock server framework based on Rack.
|
4
|
+
Cameleon (not Chameleon!) is HTTP mock/stub server framework based on Rack.
|
5
|
+
You can easily create HTTP server behaves as external system for your application.
|
5
6
|
|
6
|
-
All you have to do to create a new interface is simply make directory and put
|
7
|
+
All you have to do to create a new HTTP interface is simply make directory and put response file into the directory.
|
7
8
|
|
8
9
|
Features:
|
9
10
|
|
11
|
+
- Easy to create new HTTP interfaces (only to create directory and put response file into it)
|
10
12
|
- Simple API for "_switch.rb" (for selecting response file)
|
11
|
-
- Communicate in JSON, XML or any format over HTTP
|
12
|
-
-
|
13
|
+
- Communicate in JSON, XML or any other format over HTTP
|
14
|
+
- Ready for RESTful API
|
13
15
|
|
14
16
|
|
15
17
|
## Installation
|
@@ -19,11 +21,11 @@ You can install Cameleon using RubyGems as below:
|
|
19
21
|
|
20
22
|
|
21
23
|
## Getting started
|
22
|
-
First, create a new Cameleon project.
|
24
|
+
First, create a new Cameleon project using 'cameleon new' command.
|
23
25
|
|
24
|
-
$ cameleon new your_project_name
|
26
|
+
$ cameleon new your_project_name
|
25
27
|
|
26
|
-
|
28
|
+
Move into the directory you've created now, and start cameleon server.
|
27
29
|
|
28
30
|
$ cd your_project_name
|
29
31
|
$ cameleon server
|
@@ -43,7 +45,7 @@ Think about creating HTTP interface such like below:
|
|
43
45
|
- URL: /path/to/interface
|
44
46
|
- response: "ok"
|
45
47
|
|
46
|
-
|
48
|
+
Follow these steps:
|
47
49
|
|
48
50
|
$ mkdir -p response/path/to/interface
|
49
51
|
$ cd response/path/to/interface
|
@@ -55,7 +57,7 @@ Test the interface:
|
|
55
57
|
ok
|
56
58
|
|
57
59
|
### generate command
|
58
|
-
|
60
|
+
You can create a interface using "cameleon generate" command.
|
59
61
|
If you want to create a new interface "/path/to/foo", type:
|
60
62
|
|
61
63
|
$ cameleon generate path/to/foo
|
@@ -65,10 +67,11 @@ and test it.
|
|
65
67
|
$ curl http://localhost:9292/path/to/foo
|
66
68
|
edit me: response/path/to/foo/default
|
67
69
|
|
68
|
-
|
70
|
+
Edit the file as you need.
|
69
71
|
|
70
72
|
### _switch.rb
|
71
|
-
If you want to change the response by HTTP request, you
|
73
|
+
If you want to change the response by HTTP request, you should create "_switch.rb" in your response directory.
|
74
|
+
"_switch.rb" should be pure Ruby file. You can write any logic you need in Ruby.
|
72
75
|
|
73
76
|
Below shows a sample of "_switch.rb".
|
74
77
|
|
@@ -85,9 +88,38 @@ Below shows a sample of "_switch.rb".
|
|
85
88
|
|
86
89
|
# if else, render "default" (any extention acceptable) file.
|
87
90
|
|
91
|
+
### Validation
|
92
|
+
Cameleon can validate HTTP request body. Currently JSON format is supported. If you want to validate your request to Cameleon, add 'validation' attribute and 'type' sub-attribute as 'json' into your 'cameleon.yml' config file.
|
93
|
+
|
94
|
+
port: 9292
|
95
|
+
validation:
|
96
|
+
type: json
|
97
|
+
|
98
|
+
then put 'request.schema.json' into response directory. If your interface allows json request body like:
|
99
|
+
|
100
|
+
{"a": 1}
|
101
|
+
|
102
|
+
You should create 'request.schema.json' like:
|
103
|
+
|
104
|
+
{
|
105
|
+
"type" : "object",
|
106
|
+
"properties" : {
|
107
|
+
"a" : {
|
108
|
+
"type" : "integer",
|
109
|
+
"required" : true
|
110
|
+
}
|
111
|
+
}
|
112
|
+
}
|
113
|
+
|
114
|
+
Cameleon uses json-schema gem as validation engine, and it's based on [JSON Schema Draft 3](http://tools.ietf.org/html/draft-zyp-json-schema-03). See [json-schema](https://github.com/hoxworth/json-schema) for more detail.
|
115
|
+
|
116
|
+
Notice:
|
117
|
+
|
118
|
+
Cameleon validates request body only in POST or PUT http method.
|
88
119
|
|
89
120
|
## Gallery
|
90
121
|
You can get various samples using "cameleon gallery" command.
|
122
|
+
|
91
123
|
$ cd YOUR_CAMELEON_HOME
|
92
124
|
$ cameleon gallery
|
93
125
|
|
data/Rakefile
CHANGED
@@ -31,23 +31,4 @@ Rake::TestTask.new(:test) do |test|
|
|
31
31
|
test.pattern = 'test/**/test_*.rb'
|
32
32
|
test.verbose = true
|
33
33
|
end
|
34
|
-
|
35
|
-
require 'rcov/rcovtask'
|
36
|
-
Rcov::RcovTask.new do |test|
|
37
|
-
test.libs << 'test'
|
38
|
-
test.pattern = 'test/**/test_*.rb'
|
39
|
-
test.verbose = true
|
40
|
-
test.rcov_opts << '--exclude "gems/*"'
|
41
|
-
end
|
42
|
-
|
43
34
|
task :default => :test
|
44
|
-
|
45
|
-
require 'rake/rdoctask'
|
46
|
-
Rake::RDocTask.new do |rdoc|
|
47
|
-
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
48
|
-
|
49
|
-
rdoc.rdoc_dir = 'rdoc'
|
50
|
-
rdoc.title = "cameleon #{version}"
|
51
|
-
rdoc.rdoc_files.include('README*')
|
52
|
-
rdoc.rdoc_files.include('lib/**/*.rb')
|
53
|
-
end
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.1.
|
1
|
+
0.1.1
|
data/cameleon.gemspec
CHANGED
data/lib/cameleon/action.rb
CHANGED
@@ -1,7 +1,14 @@
|
|
1
1
|
# -*- coding: utf-8 -*-
|
2
2
|
require 'erubis'
|
3
|
+
require 'json-schema'
|
3
4
|
|
4
5
|
class Cameleon
|
6
|
+
class ValidationError < Exception
|
7
|
+
def initialize(msg)
|
8
|
+
super(msg)
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
5
12
|
class Action
|
6
13
|
def find_default_file
|
7
14
|
Dir.entries(@base_path).find { |f| f =~ /^default\./ }
|
@@ -14,6 +21,13 @@ class Cameleon
|
|
14
21
|
end
|
15
22
|
@params = req.params
|
16
23
|
@body = req.body.read
|
24
|
+
if ["POST", "PUT"].include? req.request_method
|
25
|
+
begin
|
26
|
+
validate_request_body
|
27
|
+
rescue Cameleon::ValidationError => e
|
28
|
+
return [400, {}, [e.message]]
|
29
|
+
end
|
30
|
+
end
|
17
31
|
switch_filepath = File.join @base_path, "_switch.rb"
|
18
32
|
renderer = nil
|
19
33
|
if !File.exists?(@base_path)
|
@@ -25,5 +39,29 @@ class Cameleon
|
|
25
39
|
end
|
26
40
|
renderer.build_response
|
27
41
|
end
|
42
|
+
|
43
|
+
def validate_request_body
|
44
|
+
validation_type = Cameleon.config.validation && Cameleon.config.validation.type
|
45
|
+
case validation_type
|
46
|
+
when "json"
|
47
|
+
validate_json
|
48
|
+
when nil
|
49
|
+
else
|
50
|
+
raise "unknown validation type '#{validation_type}' in config.yml"
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
def validate_json
|
55
|
+
schema_filepath = File.join(@base_path, "request.schema.json")
|
56
|
+
if File.exists? schema_filepath
|
57
|
+
begin
|
58
|
+
schema = JSON.parse File.read(schema_filepath)
|
59
|
+
JSON::Validator.validate!(schema, @body)
|
60
|
+
rescue JSON::Schema::ValidationError => e
|
61
|
+
error_body = {"error" => e.message}.to_json
|
62
|
+
raise Cameleon::ValidationError.new(error_body)
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
28
66
|
end
|
29
67
|
end
|
@@ -11,5 +11,9 @@ when "bar"
|
|
11
11
|
render "bar.json"
|
12
12
|
end
|
13
13
|
|
14
|
+
# when you enable JSON validation (edit cameleon.yml)
|
15
|
+
# you may receive error if you post invalid request
|
16
|
+
# curl -XPOST http://localhost:9292/gallery/json -d"{\"invalid\":\"format\"}"
|
17
|
+
|
14
18
|
# if nothing to match, use 404 HTTP Status Code and render default.json.
|
15
19
|
status_code 404
|
data/template/new/cameleon.yml
CHANGED
metadata
CHANGED
@@ -1,115 +1,141 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: cameleon
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
5
|
-
prerelease:
|
4
|
+
version: 0.1.1
|
6
5
|
platform: ruby
|
7
6
|
authors:
|
8
7
|
- daixque
|
9
8
|
autorequire:
|
10
9
|
bindir: bin
|
11
10
|
cert_chain: []
|
12
|
-
date: 2011-09-11 00:00:00.
|
11
|
+
date: 2011-09-11 00:00:00.000000000 Z
|
13
12
|
dependencies:
|
14
13
|
- !ruby/object:Gem::Dependency
|
15
14
|
name: erubis
|
16
|
-
requirement:
|
17
|
-
none: false
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
18
16
|
requirements:
|
19
17
|
- - ! '>='
|
20
18
|
- !ruby/object:Gem::Version
|
21
19
|
version: '0'
|
22
20
|
type: :runtime
|
23
21
|
prerelease: false
|
24
|
-
version_requirements:
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ! '>='
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
25
27
|
- !ruby/object:Gem::Dependency
|
26
28
|
name: rack
|
27
|
-
requirement:
|
28
|
-
none: false
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
29
30
|
requirements:
|
30
31
|
- - ! '>='
|
31
32
|
- !ruby/object:Gem::Version
|
32
33
|
version: '0'
|
33
34
|
type: :runtime
|
34
35
|
prerelease: false
|
35
|
-
version_requirements:
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ! '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
36
41
|
- !ruby/object:Gem::Dependency
|
37
42
|
name: hashie
|
38
|
-
requirement:
|
39
|
-
none: false
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
40
44
|
requirements:
|
41
45
|
- - ! '>='
|
42
46
|
- !ruby/object:Gem::Version
|
43
47
|
version: '0'
|
44
48
|
type: :runtime
|
45
49
|
prerelease: false
|
46
|
-
version_requirements:
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ! '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
47
55
|
- !ruby/object:Gem::Dependency
|
48
56
|
name: json
|
49
|
-
requirement:
|
50
|
-
none: false
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
51
58
|
requirements:
|
52
59
|
- - ! '>='
|
53
60
|
- !ruby/object:Gem::Version
|
54
61
|
version: '0'
|
55
62
|
type: :runtime
|
56
63
|
prerelease: false
|
57
|
-
version_requirements:
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ! '>='
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
58
69
|
- !ruby/object:Gem::Dependency
|
59
70
|
name: nokogiri
|
60
|
-
requirement:
|
61
|
-
none: false
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
62
72
|
requirements:
|
63
73
|
- - ! '>='
|
64
74
|
- !ruby/object:Gem::Version
|
65
75
|
version: '0'
|
66
76
|
type: :runtime
|
67
77
|
prerelease: false
|
68
|
-
version_requirements:
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ! '>='
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
69
83
|
- !ruby/object:Gem::Dependency
|
70
84
|
name: shoulda
|
71
|
-
requirement:
|
72
|
-
none: false
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
73
86
|
requirements:
|
74
87
|
- - ! '>='
|
75
88
|
- !ruby/object:Gem::Version
|
76
89
|
version: '0'
|
77
90
|
type: :development
|
78
91
|
prerelease: false
|
79
|
-
version_requirements:
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ! '>='
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
80
97
|
- !ruby/object:Gem::Dependency
|
81
98
|
name: bundler
|
82
|
-
requirement:
|
83
|
-
none: false
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
84
100
|
requirements:
|
85
101
|
- - ~>
|
86
102
|
- !ruby/object:Gem::Version
|
87
103
|
version: 1.0.0
|
88
104
|
type: :development
|
89
105
|
prerelease: false
|
90
|
-
version_requirements:
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - ~>
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: 1.0.0
|
91
111
|
- !ruby/object:Gem::Dependency
|
92
112
|
name: jeweler
|
93
|
-
requirement:
|
94
|
-
none: false
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
95
114
|
requirements:
|
96
115
|
- - ~>
|
97
116
|
- !ruby/object:Gem::Version
|
98
117
|
version: 1.6.4
|
99
118
|
type: :development
|
100
119
|
prerelease: false
|
101
|
-
version_requirements:
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - ~>
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: 1.6.4
|
102
125
|
- !ruby/object:Gem::Dependency
|
103
126
|
name: rcov
|
104
|
-
requirement:
|
105
|
-
none: false
|
127
|
+
requirement: !ruby/object:Gem::Requirement
|
106
128
|
requirements:
|
107
129
|
- - ! '>='
|
108
130
|
- !ruby/object:Gem::Version
|
109
131
|
version: '0'
|
110
132
|
type: :development
|
111
133
|
prerelease: false
|
112
|
-
version_requirements:
|
134
|
+
version_requirements: !ruby/object:Gem::Requirement
|
135
|
+
requirements:
|
136
|
+
- - ! '>='
|
137
|
+
- !ruby/object:Gem::Version
|
138
|
+
version: '0'
|
113
139
|
description: Cameleon is HTTP mock server framework based on Rack. You can easily
|
114
140
|
create HTTP server for mockup or stub of external system for your application.
|
115
141
|
email: daixque@gmail.com
|
@@ -165,28 +191,24 @@ files:
|
|
165
191
|
homepage: http://github.com/daixque/cameleon
|
166
192
|
licenses:
|
167
193
|
- MIT
|
194
|
+
metadata: {}
|
168
195
|
post_install_message:
|
169
196
|
rdoc_options: []
|
170
197
|
require_paths:
|
171
198
|
- lib
|
172
199
|
required_ruby_version: !ruby/object:Gem::Requirement
|
173
|
-
none: false
|
174
200
|
requirements:
|
175
201
|
- - ! '>='
|
176
202
|
- !ruby/object:Gem::Version
|
177
203
|
version: '0'
|
178
|
-
segments:
|
179
|
-
- 0
|
180
|
-
hash: -2425352503709104828
|
181
204
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
182
|
-
none: false
|
183
205
|
requirements:
|
184
206
|
- - ! '>='
|
185
207
|
- !ruby/object:Gem::Version
|
186
208
|
version: '0'
|
187
209
|
requirements: []
|
188
210
|
rubyforge_project:
|
189
|
-
rubygems_version:
|
211
|
+
rubygems_version: 2.0.3
|
190
212
|
signing_key:
|
191
213
|
specification_version: 3
|
192
214
|
summary: Cameleon is HTTP mock server framework.
|