endo 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 90b76cfad1ab5f415e15a9b7f661c451844a8c18
4
- data.tar.gz: 4d858870cf13514bec7e1fdc6460def568f6d93b
3
+ metadata.gz: 7a6e0eb3c683feb92b2e6b18a1a76ff6354a99e3
4
+ data.tar.gz: 2f97c729f144198dc2cb4d840415c746ac19ae83
5
5
  SHA512:
6
- metadata.gz: fe230bbd9feb1d12d59eae5ced3de6ffa5011a233336acce5234ada09a66038c2622559fa2e023b07748488325968bfd82a579b72d809879595159b74cfb4279
7
- data.tar.gz: 82fb6f79d151d610f39a1a317fca3fd3c55b1b1fe908337ba2c77b8b9d4f9327c5b78b9c75c549c3fde6614373186976b52f859b2b927200ac7689ced7c14db4
6
+ metadata.gz: c79bbe2c0263bb186da2dfc9f603d97fae2e7db20de7a5185508fdc2dc389b393ac8da50422c71eefcac213db2da59177853aafcf6d5d1f5d39989c4d3671e82
7
+ data.tar.gz: 9c1520a0698c7191fdb64e05a0549f2fafee9d570a8639084d0048efaa16327bef551429685c156f872dd936f769b084d2844129ca389fce1975ad4b5a5b0854
data/endo/sample.rb ADDED
@@ -0,0 +1,50 @@
1
+ set :base_url, 'http://localhost:3000'
2
+ basic_auth 'user', 'pass'
3
+
4
+ get '/articles' do
5
+ expect(header: 'Content-Type').to eq 'application/json; charset=utf-8'
6
+ end
7
+
8
+ post '/articles.json' do
9
+ param 'article[title]', 'hello'
10
+ param 'article[content]', 'Hello, world!'
11
+ end
12
+
13
+ get '/articles/:article_id' do
14
+ param :article_id do
15
+ from :post, '/articles.json', ->{ self[:id] }
16
+ end
17
+
18
+ expect(header: 'Content-Type').to eq 'application/json; charset=utf-8'
19
+ expect(body: ->{ self[:title] }).to eq 'hello'
20
+ end
21
+
22
+ patch '/articles/:article_id.json' do
23
+ param :article_id do
24
+ from :post, '/articles.json', ->{ self[:id] }
25
+ end
26
+
27
+ param 'article[title]', 'こんにちは'
28
+ end
29
+
30
+ get '/articles/:article_id' do
31
+ param :article_id do
32
+ from :post, '/articles.json', ->{ self[:id] }
33
+ end
34
+
35
+ expect(body: ->{ self[:title] }).to eq 'こんにちは'
36
+ end
37
+
38
+ put '/articles/:article_id.json' do
39
+ param :article_id do
40
+ from :post, '/articles.json', ->{ self[:id] }
41
+ end
42
+
43
+ param 'article[title]', 'bonjour'
44
+ end
45
+
46
+ delete '/articles/:article_id.json' do
47
+ param :article_id do
48
+ from :post, '/articles.json', ->{ self[:id] }
49
+ end
50
+ end
data/lib/endo/cli.rb CHANGED
@@ -8,7 +8,7 @@ module Endo
8
8
  desc "exec usage", "exec desc"
9
9
  def exec(endo_file=nil)
10
10
  if endo_file.nil?
11
- Dir.glob('endo/*.endo').each do |f|
11
+ Dir.glob('endo/*.rb').each do |f|
12
12
  exec_proc(f)
13
13
  end
14
14
  else
@@ -27,4 +27,4 @@ module Endo
27
27
  executor.instance_eval File.read(file_path)
28
28
  end
29
29
  end
30
- end
30
+ end
data/lib/endo/core.rb CHANGED
@@ -26,6 +26,18 @@ module Endo
26
26
  request(endpoint, :post, &block)
27
27
  end
28
28
 
29
+ def delete(endpoint, &block)
30
+ request(endpoint, :delete, &block)
31
+ end
32
+
33
+ def patch(endpoint, &block)
34
+ request(endpoint, :patch, &block)
35
+ end
36
+
37
+ def put(endpoint, &block)
38
+ request(endpoint, :put, &block)
39
+ end
40
+
29
41
  #TODO: 制限
30
42
  def param(key, val=nil, &block)
31
43
  unless (!val.nil?) ^ (!block.nil?) # Only either one
@@ -63,6 +75,12 @@ module Endo
63
75
  val
64
76
  end
65
77
 
78
+ def basic_auth(user, pass)
79
+ @basic_auth = {
80
+ user: user, pass: pass
81
+ }
82
+ end
83
+
66
84
  private
67
85
  def request(endpoint, method, &block)
68
86
  org_endpoint = endpoint.clone
@@ -83,12 +101,12 @@ module Endo
83
101
  res_data = parse_body_json(res)
84
102
  @responses[build_response_key(method, endpoint)] = res_data
85
103
  validate_expects(res) unless @expect_alls.empty? && @expects.empty?
86
- message = "🍺 #{endpoint} [#{t_end-t_start}ms]"
104
+ message = "🍺 #{method.upcase} #{endpoint} [#{t_end-t_start}ms]"
87
105
  rescue Error::HttpError=>e
88
- message = "💩 #{endpoint} [code: #{e.code}]".red
106
+ message = "💩 #{method.upcase} #{endpoint} [code: #{e.code}]".red
89
107
  exit 1
90
108
  rescue Error::ValidationError => e
91
- message = "👮 #{endpoint} [expected: \"#{e.expected}\" actual: \"#{e.actual}\"]".yellow
109
+ message = "👮 #{method.upcase} #{endpoint} [expected: \"#{e.expected}\" actual: \"#{e.actual}\"]".yellow
92
110
  ensure
93
111
  puts message
94
112
  end
@@ -117,13 +135,27 @@ module Endo
117
135
  def http_request(url, params, method: :get)
118
136
  uri = URI.parse url
119
137
 
120
- req = if method == :get
138
+ req = case method
139
+ when :get
121
140
  uri.query = URI.encode_www_form(params)
122
- req = Net::HTTP::Get.new uri
123
- elsif method == :post
141
+ Net::HTTP::Get.new uri
142
+ when :post
124
143
  req = Net::HTTP::Post.new uri.path
125
144
  req.set_form_data(params)
126
145
  req
146
+ when :delete
147
+ uri.query = URI.encode_www_form(params)
148
+ req = Net::HTTP::Delete.new uri
149
+ when :patch
150
+ uri.query = URI.encode_www_form(params)
151
+ req = Net::HTTP::Patch.new uri
152
+ when :put
153
+ uri.query = URI.encode_www_form(params)
154
+ req = Net::HTTP::Put.new uri
155
+ end
156
+
157
+ if @basic_auth
158
+ req.basic_auth @basic_auth[:user], @basic_auth[:pass]
127
159
  end
128
160
 
129
161
  res = Net::HTTP.start(uri.host, uri.port) {|http| http.request req }
@@ -133,7 +165,7 @@ module Endo
133
165
  end
134
166
 
135
167
  def parse_body_json(res)
136
- JSON.parse(res.body, symbolize_names: true)
168
+ JSON.parse(res.body, symbolize_names: true) if res.body
137
169
  end
138
170
 
139
171
  def build_response_key(method, endpoint)
@@ -0,0 +1,16 @@
1
+ module Endo
2
+ module Matchers
3
+ class Include < BaseMatcher
4
+ attr_reader :expected
5
+
6
+ private
7
+ def match(expected, actual)
8
+ if actual.respond_to? :include?
9
+ actual.include? expected
10
+ else
11
+ raise 'NotSupported'
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
data/lib/endo/matchers.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  require 'endo/matchers/base_matcher'
2
2
  require 'endo/matchers/eq'
3
+ require 'endo/matchers/include'
3
4
 
4
5
 
5
6
  module Endo
@@ -7,5 +8,9 @@ module Endo
7
8
  def eq(expected)
8
9
  Matchers::Eq.new(expected)
9
10
  end
11
+
12
+ def include(expected)
13
+ Matchers::Include.new(expected)
14
+ end
10
15
  end
11
16
  end
data/lib/endo/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Endo
2
- VERSION = "0.2.0"
2
+ VERSION = "0.3.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: endo
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - maruware
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-03-07 00:00:00.000000000 Z
11
+ date: 2016-05-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: thor
@@ -94,7 +94,7 @@ files:
94
94
  - Rakefile
95
95
  - bin/endo
96
96
  - endo.gemspec
97
- - endo/sample.endo
97
+ - endo/sample.rb
98
98
  - lib/endo.rb
99
99
  - lib/endo/cli.rb
100
100
  - lib/endo/core.rb
@@ -105,6 +105,7 @@ files:
105
105
  - lib/endo/matchers.rb
106
106
  - lib/endo/matchers/base_matcher.rb
107
107
  - lib/endo/matchers/eq.rb
108
+ - lib/endo/matchers/include.rb
108
109
  - lib/endo/version.rb
109
110
  homepage: https://github.com/maruware/endo
110
111
  licenses: []
data/endo/sample.endo DELETED
@@ -1,18 +0,0 @@
1
- set :base_url, 'http://endo-sample.maruware.com'
2
-
3
- get '/articles' do
4
- expect(body: ->{ at(0)[:title] }).to eq 'good'
5
- end
6
-
7
- get '/articles/:article_id' do
8
- param :article_id do
9
- from :get, '/articles', ->{ first[:id] }
10
- end
11
-
12
- expect(header: 'Content-Type').to eq 'application/json; charset=utf-8'
13
- end
14
-
15
- post '/articless' do
16
- param :title, 'hello'
17
- param :text, 'Hello, world!'
18
- end