http_spec 0.0.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.
@@ -0,0 +1,33 @@
1
+ require "http_spec/types"
2
+ require "rack/mock"
3
+
4
+ module HTTPSpec
5
+ module Clients
6
+ class Rack
7
+ def initialize(app)
8
+ @session = ::Rack::MockRequest.new(app)
9
+ end
10
+
11
+ def dispatch(request)
12
+ opts = headers_to_env(request.headers)
13
+ opts[:input] = request.body
14
+ from_rack @session.request(request.method, request.path, opts)
15
+ end
16
+
17
+ private
18
+
19
+ def from_rack(response)
20
+ Response.new(response.status, response.body, response.headers)
21
+ end
22
+
23
+ def headers_to_env(headers)
24
+ return {} unless headers
25
+ headers.inject({}) do |env, (k, v)|
26
+ k = k.tr("-", "_").upcase
27
+ k = "HTTP_#{k}" unless %w{CONTENT_TYPE CONTENT_LENGTH}.include?(k)
28
+ env.merge(k => v)
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,18 @@
1
+ require "http_spec/types"
2
+
3
+ module HTTPSpec
4
+ module DSL
5
+ module Methods
6
+ def self.define_actions(*methods)
7
+ methods.each do |method|
8
+ define_method(method) do |path, body=nil, headers=nil|
9
+ request = Request.new(method, path, body, headers)
10
+ client.dispatch(request)
11
+ end
12
+ end
13
+ end
14
+
15
+ define_actions :get, :post, :put, :patch, :delete, :options, :head
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,94 @@
1
+ require "http_spec/types"
2
+
3
+ module HTTPSpec
4
+ module DSL
5
+ module Resource
6
+ def self.included(base)
7
+ base.extend(ClassMethods)
8
+ end
9
+
10
+ module ClassMethods
11
+ def self.define_actions(*methods)
12
+ methods.each do |method|
13
+ define_method(method) do |route, &block|
14
+ description = "#{method.to_s.upcase} #{route}"
15
+ request = Request.new(method, route)
16
+ context(description, :request => request, &block)
17
+ end
18
+ end
19
+ end
20
+
21
+ define_actions :get, :post, :put, :patch, :delete, :options, :head
22
+
23
+ def parameter(name, description, extra = {})
24
+ copy_superclass_metadata(:parameters)
25
+ metadata[:parameters] ||= {}
26
+ metadata[:parameters][name] = extra.merge(:description => description)
27
+ end
28
+
29
+ def header(name, value)
30
+ copy_superclass_metadata(:default_headers)
31
+ metadata[:default_headers] ||= {}
32
+ metadata[:default_headers][name] = value
33
+ end
34
+
35
+ def copy_superclass_metadata(key)
36
+ return unless superclass_metadata && superclass_metadata[key]
37
+ if superclass_metadata[key].equal?(metadata[key])
38
+ metadata[key] = superclass_metadata[key].dup
39
+ end
40
+ end
41
+ end
42
+
43
+ def do_request(options = {})
44
+ request = example.metadata[:request]
45
+ request.body = options[:body]
46
+ request.headers = default_headers(options[:headers])
47
+ request.parameters = example.metadata[:parameters]
48
+ build_path(request)
49
+ @last_response = client.dispatch(request)
50
+ end
51
+
52
+ def params
53
+ return {} unless example.metadata[:parameters]
54
+ params = {}
55
+ example.metadata[:parameters].each_key do |name|
56
+ params[name] = send(name) if respond_to?(name)
57
+ end
58
+ params
59
+ end
60
+
61
+ def status
62
+ @last_response.status
63
+ end
64
+ alias response_status status
65
+
66
+ def response_headers
67
+ @last_response.headers
68
+ end
69
+
70
+ def response_body
71
+ @last_response.body
72
+ end
73
+
74
+ private
75
+
76
+ def build_path(request)
77
+ request.path.gsub!(/:(\w+)/) do |match|
78
+ if respond_to?($1)
79
+ send($1)
80
+ else
81
+ match
82
+ end
83
+ end
84
+ end
85
+
86
+ def default_headers(headers)
87
+ default_headers = example.metadata[:default_headers]
88
+ return default_headers if headers.nil?
89
+ return headers if default_headers.nil?
90
+ default_headers.merge(headers)
91
+ end
92
+ end
93
+ end
94
+ end
@@ -0,0 +1,4 @@
1
+ module HTTPSpec
2
+ Request = Struct.new(:method, :path, :body, :headers, :parameters)
3
+ Response = Struct.new(:status, :body, :headers)
4
+ end
metadata ADDED
@@ -0,0 +1,48 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: http_spec
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Sam Goldman
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-07-22 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description:
15
+ email:
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/http_spec/clients/rack.rb
21
+ - lib/http_spec/dsl/methods.rb
22
+ - lib/http_spec/dsl/resource.rb
23
+ - lib/http_spec/types.rb
24
+ homepage: https://github.com/smartlogic/http_spec
25
+ licenses: []
26
+ post_install_message:
27
+ rdoc_options: []
28
+ require_paths:
29
+ - lib
30
+ required_ruby_version: !ruby/object:Gem::Requirement
31
+ none: false
32
+ requirements:
33
+ - - ! '>='
34
+ - !ruby/object:Gem::Version
35
+ version: '0'
36
+ required_rubygems_version: !ruby/object:Gem::Requirement
37
+ none: false
38
+ requirements:
39
+ - - ! '>='
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ requirements: []
43
+ rubyforge_project:
44
+ rubygems_version: 1.8.24
45
+ signing_key:
46
+ specification_version: 3
47
+ summary: RSpec DSL for describing API behaviors
48
+ test_files: []