http-double 0.0.1 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/http-double.rb +14 -0
- data/lib/http_double/base.rb +36 -0
- data/lib/http_double/request_logger.rb +62 -0
- data/lib/http_double/thin_logging.rb +20 -0
- data/lib/http_double/version.rb +1 -1
- metadata +74 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 588746bf46fcf2a95cdc80f2dd92a4b0967bcf51
|
4
|
+
data.tar.gz: da0b030d79ac9cff01c565a5e7d72548bab8bc81
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f2b4f2e91049966762fa4680c30c98b03f8406dc2d2a7de70fc2a506f5681e7d0d841d53b535521690bab2472f4589336588a7c20a19446ea253b38767336456
|
7
|
+
data.tar.gz: 996a293170fd7b7e9484412026161fbf5a12bf47c3355d9c9836ab5cdccc5f969a4f35b3f4d47a76b529560ab0a0f8a9026a52803dd808411b1cd93d3e594548
|
data/lib/http-double.rb
CHANGED
@@ -1 +1,15 @@
|
|
1
|
+
require 'thin'
|
2
|
+
require 'sinatra/base'
|
3
|
+
require 'forwardable'
|
4
|
+
|
5
|
+
module HttpDouble
|
6
|
+
class << self
|
7
|
+
extend Forwardable
|
8
|
+
delegate [:background, :log] => :Base
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
require 'http_double/base'
|
1
13
|
require 'http_double/version'
|
14
|
+
require 'http_double/thin_logging'
|
15
|
+
require 'http_double/request_logger'
|
@@ -0,0 +1,36 @@
|
|
1
|
+
require 'sinatra/base'
|
2
|
+
|
3
|
+
module HttpDouble
|
4
|
+
class Base < Sinatra::Base
|
5
|
+
|
6
|
+
class << self
|
7
|
+
|
8
|
+
def background(addr, port, log_path: '/dev/null')
|
9
|
+
server_class = self
|
10
|
+
thread = Thread.new do
|
11
|
+
|
12
|
+
# Even if we use /dev/null, we need a logger to suppress warnings
|
13
|
+
# generated by Thin::Logging#trace_logger being undefined
|
14
|
+
log_stream = File.open(log_path, 'a')
|
15
|
+
Thin::Logging.trace_logger = Logger.new(log_stream)
|
16
|
+
|
17
|
+
Thin::Server.start(addr, port) do
|
18
|
+
use RequestLogger, server_class.log
|
19
|
+
run server_class
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
23
|
+
thread.abort_on_exception = true
|
24
|
+
thread
|
25
|
+
end
|
26
|
+
|
27
|
+
def log
|
28
|
+
@log ||= []
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
32
|
+
|
33
|
+
not_found { [404, ''] }
|
34
|
+
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,62 @@
|
|
1
|
+
require 'rack/utils'
|
2
|
+
require 'ostruct'
|
3
|
+
require 'active_support/hash_with_indifferent_access'
|
4
|
+
|
5
|
+
class HttpDouble::RequestLogger
|
6
|
+
|
7
|
+
# noinspection RubyConstantNamingConvention
|
8
|
+
IHash = ActiveSupport::HashWithIndifferentAccess
|
9
|
+
|
10
|
+
def initialize(app, log)
|
11
|
+
@app = app
|
12
|
+
@log = log
|
13
|
+
end
|
14
|
+
|
15
|
+
def call(env)
|
16
|
+
response = @app.call(env)
|
17
|
+
res = OpenStruct.new(
|
18
|
+
code: response[0],
|
19
|
+
headers: IHash.new(response[1]),
|
20
|
+
body: response[2].join
|
21
|
+
)
|
22
|
+
@log << OpenStruct.new(request: Request.new(env), response: res)
|
23
|
+
response
|
24
|
+
end
|
25
|
+
|
26
|
+
class Request
|
27
|
+
|
28
|
+
attr_reader :env, :body
|
29
|
+
|
30
|
+
def initialize(env)
|
31
|
+
@env = env
|
32
|
+
@body = env['rack.input'].read
|
33
|
+
end
|
34
|
+
|
35
|
+
def verb
|
36
|
+
env['REQUEST_METHOD'].downcase.to_sym
|
37
|
+
end
|
38
|
+
|
39
|
+
def path
|
40
|
+
env['REQUEST_PATH']
|
41
|
+
end
|
42
|
+
|
43
|
+
def [](field, index = nil)
|
44
|
+
case env['CONTENT_TYPE']
|
45
|
+
when 'application/x-www-form-urlencoded'
|
46
|
+
result = form_fields[field]
|
47
|
+
result = result[index] if result and index
|
48
|
+
result
|
49
|
+
else
|
50
|
+
raise "The content type '#{env['CONTENT_TYPE']}' doesn't support indexed access"
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
private
|
55
|
+
|
56
|
+
def form_fields
|
57
|
+
@form_fields ||= IHash.new(Rack::Utils.parse_query(body).map { |key, value| [key.to_sym, value.is_a?(Array) ? value : [value]] }.to_h)
|
58
|
+
end
|
59
|
+
|
60
|
+
end
|
61
|
+
|
62
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
# This patch prevents Thin's logger from wigging out when you feed it binary data.
|
2
|
+
|
3
|
+
module Thin::Logging
|
4
|
+
class << self
|
5
|
+
alias_method :trace_msg_raw, :trace_msg
|
6
|
+
|
7
|
+
def trace_msg(msg)
|
8
|
+
source = msg.split "\r\n"
|
9
|
+
result = []
|
10
|
+
result << source.shift while source.first =~ /\A[[:print:]]+\z/
|
11
|
+
result << source.shift if source.first == ''
|
12
|
+
if source.index { |line| line =~ /[^[:print:]]/ }
|
13
|
+
result << '[Binary body, %s byte(s)]' % source.join("\r\n").length
|
14
|
+
else
|
15
|
+
result.push *source
|
16
|
+
end
|
17
|
+
trace_msg_raw result.join "\n"
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
data/lib/http_double/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: http-double
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Neil E. Pearson
|
@@ -10,6 +10,62 @@ bindir: bin
|
|
10
10
|
cert_chain: []
|
11
11
|
date: 2014-06-06 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: sinatra
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.4'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.4'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rack
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.5'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.5'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: thin
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '1.6'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '1.6'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: activesupport
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '4.1'
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '4.1'
|
13
69
|
- !ruby/object:Gem::Dependency
|
14
70
|
name: bundler
|
15
71
|
requirement: !ruby/object:Gem::Requirement
|
@@ -38,6 +94,20 @@ dependencies:
|
|
38
94
|
- - ">="
|
39
95
|
- !ruby/object:Gem::Version
|
40
96
|
version: '10.1'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: rspec
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - '='
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: 3.0.0.beta2
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - '='
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: 3.0.0.beta2
|
41
111
|
description: Provides a simple way to double HTTP services, APIs etc, for testing.
|
42
112
|
email:
|
43
113
|
- neil@helium.net.au
|
@@ -48,6 +118,9 @@ files:
|
|
48
118
|
- LICENSE.txt
|
49
119
|
- README.md
|
50
120
|
- lib/http-double.rb
|
121
|
+
- lib/http_double/base.rb
|
122
|
+
- lib/http_double/request_logger.rb
|
123
|
+
- lib/http_double/thin_logging.rb
|
51
124
|
- lib/http_double/version.rb
|
52
125
|
homepage: https://github.com/hx/http-double
|
53
126
|
licenses:
|