|
| 1 | +require 'net/http' |
| 2 | +require 'test_helper' |
| 3 | + |
| 4 | +class ProxyServerTest < LambySpec |
| 5 | + let(:event) { TestHelpers::Events::HttpV2.create } |
| 6 | + let(:context) { TestHelpers::LambdaContext.raw_data } |
| 7 | + let(:rack_app) { Rack::Builder.new { run lambda { |env| [200, {}, StringIO.new('{"statusCode": 200}')] } }.to_app } |
| 8 | + let(:proxy) { Lamby::ProxyServer.new } |
| 9 | + |
| 10 | + before { Lamby.config.rack_app = rack_app } |
| 11 | + |
| 12 | + it 'should return a 405 helpful message on GET' do |
| 13 | + response = proxy.call(env("REQUEST_METHOD" => 'GET')) |
| 14 | + expect(response[:statusCode]).must_equal 405 |
| 15 | + expect(response[:headers]).must_equal({"Content-Type" => "text/html"}) |
| 16 | + expect(response[:body]).must_include 'Method Not Allowed' |
| 17 | + end |
| 18 | + |
| 19 | + it 'should call Lamby.cmd on POST' do |
| 20 | + response = proxy.call(env) |
| 21 | + expect(response[:statusCode]).must_equal 200 |
| 22 | + expect(response[:headers]).must_equal({}) |
| 23 | + expect(response[:body]).must_equal '{"statusCode": 200}' |
| 24 | + end |
| 25 | + |
| 26 | + private |
| 27 | + |
| 28 | + def env(options={}) |
| 29 | + json = {"event": event, "context": context}.to_json |
| 30 | + { 'REQUEST_METHOD' => 'POST', |
| 31 | + 'PATH_INFO' => '/', |
| 32 | + 'QUERY_STRING' => '', |
| 33 | + 'SERVER_NAME' => 'localhost', |
| 34 | + 'SERVER_PORT' => '3000', |
| 35 | + 'HTTP_VERSION' => 'HTTP/1.1', |
| 36 | + 'rack.version' => Rack::VERSION, |
| 37 | + 'rack.input' => StringIO.new(json), |
| 38 | + 'rack.url_scheme' => 'http', |
| 39 | + 'rack.errors' => $stderr, |
| 40 | + 'rack.multithread' => true, |
| 41 | + 'rack.multiprocess' => false, |
| 42 | + 'rack.run_once' => false, |
| 43 | + 'CONTENT_TYPE' => 'application/json', |
| 44 | + 'CONTENT_LENGTH' => json.bytesize.to_s |
| 45 | + }.merge(options) |
| 46 | + end |
| 47 | +end |
0 commit comments