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