forked from postrank-labs/goliath
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttp_log_spec.rb
More file actions
138 lines (114 loc) · 3.72 KB
/
Copy pathhttp_log_spec.rb
File metadata and controls
138 lines (114 loc) · 3.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
require 'spec_helper'
require File.join(File.dirname(__FILE__), '../../', 'examples/http_log')
class Responder < Goliath::API
use Goliath::Rack::Params
def on_headers(env, headers)
env['client-headers'] = headers
end
def response(env)
query_params = env.params.collect { |param| param.join(": ") }
query_headers = env['client-headers'].collect { |param| param.join(": ") }
headers = {"Special" => "Header",
"Params" => query_params.join("|"),
"Path" => env[Goliath::Request::REQUEST_PATH],
"Headers" => query_headers.join("|"),
"Method" => env[Goliath::Request::REQUEST_METHOD]}
[200, headers, "Hello from Responder"]
end
end
describe HttpLog do
include Goliath::TestHelper
let(:err) { Proc.new { |c| fail "HTTP Request failed #{c.response}" } }
def config_file
File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'examples', 'config', 'http_log.rb'))
end
let(:api_options) { { :config => config_file } }
def mock_mongo(api)
api.config['mongo'] = mock('mongo').as_null_object
end
it 'responds to requests' do
with_api(HttpLog, api_options) do |api|
server(Responder, 8080)
mock_mongo(api)
get_request({}, err) do |c|
c.response_header.status.should == 200
end
end
end
it 'forwards to our API server' do
with_api(HttpLog, api_options) do |api|
server(Responder, 8080)
mock_mongo(api)
get_request({}, err) do |c|
c.response_header.status.should == 200
c.response_header['SPECIAL'].should == 'Header'
c.response.should == 'Hello from Responder'
end
end
end
context 'HTTP header handling' do
it 'transforms back properly' do
hl = HttpLog.new
hl.to_http_header("SPECIAL").should == 'Special'
hl.to_http_header("CONTENT_TYPE").should == 'Content-Type'
end
end
context 'query parameters' do
it 'forwards the query parameters' do
with_api(HttpLog, api_options) do |api|
server(Responder, 8080)
mock_mongo(api)
get_request({:query => {:first => :foo, :second => :bar, :third => :baz}}, err) do |c|
c.response_header.status.should == 200
c.response_header["PARAMS"].should == "first: foo|second: bar|third: baz"
end
end
end
end
context 'request path' do
it 'forwards the request path' do
with_api(HttpLog, api_options) do |api|
server(Responder, 8080)
mock_mongo(api)
get_request({:path => '/my/request/path'}, err) do |c|
c.response_header.status.should == 200
c.response_header['PATH'].should == '/my/request/path'
end
end
end
end
context 'headers' do
it 'forwards the headers' do
with_api(HttpLog, api_options) do |api|
server(Responder, 8080)
mock_mongo(api)
get_request({:head => {:first => :foo, :second => :bar}}, err) do |c|
c.response_header.status.should == 200
c.response_header["HEADERS"].should =~ /First: foo\|Second: bar/
end
end
end
end
context 'request method' do
it 'forwards GET requests' do
with_api(HttpLog, api_options) do |api|
server(Responder, 8080)
mock_mongo(api)
get_request({}, err) do |c|
c.response_header.status.should == 200
c.response_header["METHOD"].should == "GET"
end
end
end
it 'forwards POST requests' do
with_api(HttpLog, api_options) do |api|
server(Responder, 8080)
mock_mongo(api)
post_request({}, err) do |c|
c.response_header.status.should == 200
c.response_header["METHOD"].should == "POST"
end
end
end
end
end