spec/unit/server_spec.rb in hatetepe-0.5.2 vs spec/unit/server_spec.rb in hatetepe-0.6.0.pre
- old
+ new
@@ -1,127 +1,92 @@
-# -*- encoding: utf-8 -*-
+# encoding: utf-8
-require "spec_helper"
-require "hatetepe/server"
-require "rack/lint"
+require 'spec_helper'
-describe Hatetepe::Server, "(public API)" do
- describe ".start(config, &app)" do
- it "starts a server that listens on the supplied interface and port"
- it "passes the config to incoming connections"
- it "uses the passed block as app if any was passed"
- end
+describe Hatetepe::Server do
+ let(:server) { described_class.new(config, connection) }
- describe ".stop" do
- it "waits for all requests to finish"
- it "stops the server"
+ let(:config) do
+ { address: localhost, port: random_port, handlers: [handler_class] }
end
+ let(:connection) do
+ double('connection', parse: nil,
+ closed: double('closed', then: nil),
+ serialize: nil,
+ close: nil)
+ end
+ let(:handler_class) { double('handler_class', new: handler) }
+ let(:handler) { double('handler', post_init: nil, respond: nil) }
- describe ".stop!" do
- it "immediately stops the server"
+ describe '.start' do
+ specify { pending }
end
- describe "config[:app].call(env)" do
- let :subject do
- Object.new.tap do |s|
- s.extend Hatetepe::Server
- s.stub({
- :config => {
- :host => "127.0.5.1",
- :port => 3000,
- :app => app
- },
- :send_data => nil
- })
- s.stub(:comm_inactivity_timeout=)
- s.stub(:close_connection)
- s.post_init
- end
- end
+ describe '#initialize' do
+ subject! { server }
- let :app do
- double("app", :call => nil)
- end
+ its(:config) { should be(config) }
- let :http_request do
- [
- "POST /foo/bar?key=value HTTP/1.1",
- "Host: themachine.local",
- "Content-Length: 13",
- "",
- "Hello, world!"
- ].join("\r\n")
+ it 'sets up connection' do
+ expect(connection).to have_received(:parse)
+ .with(server.method(:serve))
+ expect(connection.closed).to have_received(:then)
+ .with(server.method(:shutdown))
end
- let :http_response do
- [
- "HTTP/1.1 403 Forbidden",
- "Content-Type: text/plain",
- "Transfer-Encoding: chunked",
- "",
- "b",
- "Mmh, nöö.",
- "0",
- "",
- ""
- ].join("\r\n")
+ it 'sets up handlers' do
+ expect(handler_class).to have_received(:new)
+ .with(config, server, connection)
+ expect(handler).to have_received(:post_init)
end
+ end
- it "receives the Rack Env hash as parameter" do
- app.should_receive :call do |env|
- Rack::Lint.new(app).check_env(env)
- env["REQUEST_METHOD"].should == "POST"
- env["REQUEST_URI"].should == "/foo/bar?key=value"
- env["HTTP_HOST"].should == "themachine.local"
- env["rack.input"].read.should == "Hello, world!"
- [ 200, {}, [] ]
- end
+ describe '#serve' do
+ let(:request) { default_request }
+ let(:response) { default_response }
- subject.receive_data(http_request)
+ before do
+ allow(server).to receive(:respond)
+ allow(handler).to receive(:serve) { @fiber = Fiber.current }
end
- it "returns a response array that will be sent to the client" do
- app.should_receive :call do |env|
- [ 403, { "Content-Type" => "text/plain" }, [ "Mmh, nöö." ] ]
- end
+ subject! { server.serve(request) }
- sent = ""
- subject.stub(:send_data) {|data| sent << data }
-
- subject.receive_data(http_request)
- sent.should == http_response
+ it 'sets up correlation' do
+ request.served.fulfill(response)
+ tick
+ expect(server).to have_received(:respond).with(request, response)
end
- it "returns asynchronously" do
- app.should_receive :call do |env|
- EM::Synchrony.add_timer 0.5 do
- response = [ 403, { "Content-Type" => "text/plain" }, [ "Mmh, nöö." ] ]
- env["async.callback"].call(response)
- end
- [ -1, {}, [] ]
- end
-
- sent = ""
- subject.stub(:send_data) {|data| sent << data }
-
- subject.receive_data(http_request)
- EM::Synchrony.sleep(0.55)
- sent.should == http_response
+ it 'notifies the handlers' do
+ expect(handler).to have_received(:serve).with(request)
+ expect(@fiber).not_to be(Fiber.current)
end
end
-end
-describe Hatetepe::Server, "(EventMachine/sermipublic API)" do
- describe "#initialize(config)"
+ describe '#respond' do
+ let(:request) { default_request }
+ let(:response) { default_response }
- describe "#post_init"
+ subject! { server.respond(request, response) }
- describe "#receive_data(data)"
+ it 'writes response to underlying connection' do
+ expect(connection).to have_received(:serialize).with(response)
+ end
- describe "#unbind(reason)"
-end
+ it 'notifies the handlers' do
+ expect(handler).to have_received(:respond).with(request, response)
+ end
+ end
-describe Hatetepe::Server, "(private API)" do
- describe "#process_request(request)"
+ describe '#close' do
+ subject! { server.close }
- describe "#send_response(response)"
+ it 'closes the underlying connection' do
+ expect(connection).to have_received(:close)
+ end
+ end
+
+ describe '#teardown' do
+ specify { pending }
+ end
end