blob: 9475a4e8f54989784bae6802271fa13044e51d7c [file] [log] [blame]
Kevin Clark080dd872008-06-18 01:10:05 +00001require File.dirname(__FILE__) + '/spec_helper'
2require 'thrift/server/httpserver'
3
4class ThriftHTTPServerSpec < Spec::ExampleGroup
5 include Thrift
6
7 Handler = SimpleMongrelHTTPServer::Handler
8
9 describe SimpleMongrelHTTPServer do
10 it "should have appropriate defaults" do
11 mock_factory = mock("BinaryProtocolFactory")
12 mock_proc = mock("Processor")
13 BinaryProtocolFactory.should_receive(:new).and_return(mock_factory)
14 Mongrel::HttpServer.should_receive(:new).with("0.0.0.0", 80).and_return do
15 mock("Mongrel::HttpServer").tee do |mock|
16 handler = mock("Handler")
17 Handler.should_receive(:new).with(mock_proc, mock_factory).and_return(handler)
18 mock.should_receive(:register).with("/", handler)
19 end
20 end
21 SimpleMongrelHTTPServer.new(mock_proc)
22 end
23
24 it "should understand :ip, :port, :path, and :protocol_factory" do
25 mock_proc = mock("Processor")
26 mock_factory = mock("ProtocolFactory")
27 Mongrel::HttpServer.should_receive(:new).with("1.2.3.4", 1234).and_return do
28 mock("Mongrel::HttpServer").tee do |mock|
29 handler = mock("Handler")
30 Handler.should_receive(:new).with(mock_proc, mock_factory).and_return(handler)
31 mock.should_receive(:register).with("/foo", handler)
32 end
33 end
34 SimpleMongrelHTTPServer.new(mock_proc, :ip => "1.2.3.4", :port => 1234, :path => "foo",
35 :protocol_factory => mock_factory)
36 end
37
38 it "should serve using Mongrel::HttpServer" do
39 BinaryProtocolFactory.stub!(:new)
40 Mongrel::HttpServer.should_receive(:new).and_return do
41 mock("Mongrel::HttpServer").tee do |mock|
42 Handler.stub!(:new)
43 mock.stub!(:register)
44 mock.should_receive(:run).and_return do
45 mock("Mongrel::HttpServer.run").tee do |runner|
46 runner.should_receive(:join)
47 end
48 end
49 end
50 end
51 SimpleMongrelHTTPServer.new(nil).serve
52 end
53 end
54
55 describe SimpleMongrelHTTPServer::Handler do
56 before(:each) do
57 @processor = mock("Processor")
58 @factory = mock("ProtocolFactory")
59 @handler = Handler.new(@processor, @factory)
60 end
61
62 it "should return 404 for non-POST requests" do
63 request = mock("request", :params => {"REQUEST_METHOD" => "GET"})
64 response = mock("response")
65 response.should_receive(:start).with(404)
66 response.should_not_receive(:start).with(200)
67 @handler.process(request, response)
68 end
69
70 it "should serve using application/x-thrift" do
71 request = mock("request", :params => {"REQUEST_METHOD" => "POST"}, :body => nil)
72 response = mock("response")
73 head = mock("head")
74 head.should_receive(:[]=).with("Content-Type", "application/x-thrift")
75 IOStreamTransport.stub!(:new)
76 @factory.stub!(:get_protocol)
77 @processor.stub!(:process)
78 response.should_receive(:start).with(200).and_yield(head, nil)
79 @handler.process(request, response)
80 end
81
82 it "should use the IOStreamTransport" do
83 body = mock("body")
84 request = mock("request", :params => {"REQUEST_METHOD" => "POST"}, :body => body)
85 response = mock("response")
86 head = mock("head")
87 head.stub!(:[]=)
88 out = mock("out")
89 protocol = mock("protocol")
90 transport = mock("transport")
91 IOStreamTransport.should_receive(:new).with(body, out).and_return(transport)
92 @factory.should_receive(:get_protocol).with(transport).and_return(protocol)
93 @processor.should_receive(:process).with(protocol, protocol)
94 response.should_receive(:start).with(200).and_yield(head, out)
95 @handler.process(request, response)
96 end
97 end
98end