David Reiss | ea2cba8 | 2009-03-30 21:35:00 +0000 | [diff] [blame^] | 1 | # |
| 2 | # Licensed to the Apache Software Foundation (ASF) under one |
| 3 | # or more contributor license agreements. See the NOTICE file |
| 4 | # distributed with this work for additional information |
| 5 | # regarding copyright ownership. The ASF licenses this file |
| 6 | # to you under the Apache License, Version 2.0 (the |
| 7 | # "License"); you may not use this file except in compliance |
| 8 | # with the License. You may obtain a copy of the License at |
| 9 | # |
| 10 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | # |
| 12 | # Unless required by applicable law or agreed to in writing, |
| 13 | # software distributed under the License is distributed on an |
| 14 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | # KIND, either express or implied. See the License for the |
| 16 | # specific language governing permissions and limitations |
| 17 | # under the License. |
| 18 | # |
| 19 | |
Kevin Clark | 080dd87 | 2008-06-18 01:10:05 +0000 | [diff] [blame] | 20 | require File.dirname(__FILE__) + '/spec_helper' |
| 21 | require 'thrift/server/httpserver' |
| 22 | |
| 23 | class ThriftHTTPServerSpec < Spec::ExampleGroup |
| 24 | include Thrift |
| 25 | |
| 26 | Handler = SimpleMongrelHTTPServer::Handler |
| 27 | |
| 28 | describe SimpleMongrelHTTPServer do |
| 29 | it "should have appropriate defaults" do |
| 30 | mock_factory = mock("BinaryProtocolFactory") |
| 31 | mock_proc = mock("Processor") |
| 32 | BinaryProtocolFactory.should_receive(:new).and_return(mock_factory) |
| 33 | Mongrel::HttpServer.should_receive(:new).with("0.0.0.0", 80).and_return do |
| 34 | mock("Mongrel::HttpServer").tee do |mock| |
| 35 | handler = mock("Handler") |
| 36 | Handler.should_receive(:new).with(mock_proc, mock_factory).and_return(handler) |
| 37 | mock.should_receive(:register).with("/", handler) |
| 38 | end |
| 39 | end |
| 40 | SimpleMongrelHTTPServer.new(mock_proc) |
| 41 | end |
| 42 | |
| 43 | it "should understand :ip, :port, :path, and :protocol_factory" do |
| 44 | mock_proc = mock("Processor") |
| 45 | mock_factory = mock("ProtocolFactory") |
| 46 | Mongrel::HttpServer.should_receive(:new).with("1.2.3.4", 1234).and_return do |
| 47 | mock("Mongrel::HttpServer").tee do |mock| |
| 48 | handler = mock("Handler") |
| 49 | Handler.should_receive(:new).with(mock_proc, mock_factory).and_return(handler) |
| 50 | mock.should_receive(:register).with("/foo", handler) |
| 51 | end |
| 52 | end |
| 53 | SimpleMongrelHTTPServer.new(mock_proc, :ip => "1.2.3.4", :port => 1234, :path => "foo", |
| 54 | :protocol_factory => mock_factory) |
| 55 | end |
| 56 | |
| 57 | it "should serve using Mongrel::HttpServer" do |
| 58 | BinaryProtocolFactory.stub!(:new) |
| 59 | Mongrel::HttpServer.should_receive(:new).and_return do |
| 60 | mock("Mongrel::HttpServer").tee do |mock| |
| 61 | Handler.stub!(:new) |
| 62 | mock.stub!(:register) |
| 63 | mock.should_receive(:run).and_return do |
| 64 | mock("Mongrel::HttpServer.run").tee do |runner| |
| 65 | runner.should_receive(:join) |
| 66 | end |
| 67 | end |
| 68 | end |
| 69 | end |
| 70 | SimpleMongrelHTTPServer.new(nil).serve |
| 71 | end |
| 72 | end |
| 73 | |
| 74 | describe SimpleMongrelHTTPServer::Handler do |
| 75 | before(:each) do |
| 76 | @processor = mock("Processor") |
| 77 | @factory = mock("ProtocolFactory") |
| 78 | @handler = Handler.new(@processor, @factory) |
| 79 | end |
| 80 | |
| 81 | it "should return 404 for non-POST requests" do |
| 82 | request = mock("request", :params => {"REQUEST_METHOD" => "GET"}) |
| 83 | response = mock("response") |
| 84 | response.should_receive(:start).with(404) |
| 85 | response.should_not_receive(:start).with(200) |
| 86 | @handler.process(request, response) |
| 87 | end |
| 88 | |
| 89 | it "should serve using application/x-thrift" do |
| 90 | request = mock("request", :params => {"REQUEST_METHOD" => "POST"}, :body => nil) |
| 91 | response = mock("response") |
| 92 | head = mock("head") |
| 93 | head.should_receive(:[]=).with("Content-Type", "application/x-thrift") |
| 94 | IOStreamTransport.stub!(:new) |
| 95 | @factory.stub!(:get_protocol) |
| 96 | @processor.stub!(:process) |
| 97 | response.should_receive(:start).with(200).and_yield(head, nil) |
| 98 | @handler.process(request, response) |
| 99 | end |
| 100 | |
| 101 | it "should use the IOStreamTransport" do |
| 102 | body = mock("body") |
| 103 | request = mock("request", :params => {"REQUEST_METHOD" => "POST"}, :body => body) |
| 104 | response = mock("response") |
| 105 | head = mock("head") |
| 106 | head.stub!(:[]=) |
| 107 | out = mock("out") |
| 108 | protocol = mock("protocol") |
| 109 | transport = mock("transport") |
| 110 | IOStreamTransport.should_receive(:new).with(body, out).and_return(transport) |
| 111 | @factory.should_receive(:get_protocol).with(transport).and_return(protocol) |
| 112 | @processor.should_receive(:process).with(protocol, protocol) |
| 113 | response.should_receive(:start).with(200).and_yield(head, out) |
| 114 | @handler.process(request, response) |
| 115 | end |
| 116 | end |
| 117 | end |