spec/integration_spec.rb in nyara-0.0.1.pre.8 vs spec/integration_spec.rb in nyara-0.0.1.pre.9

- old
+ new

@@ -1,8 +1,14 @@ require_relative "spec_helper" class TestController < Nyara::Controller + attr_reader :before_invoked + + before ":delete" do + @before_invoked = true + end + meta '#index' get '/' do content_type 'txt' send_string '初めまして from test' end @@ -10,17 +16,32 @@ meta '#create' post '/create' do redirect_to '#index' end + post '/upload' do + params + # no response + end + put '/send_file/%z' do |name| send_file Nyara.config.views_path name end delete '/render' do render 'edit.slim' end + + patch '/stream' do + view = stream 'edit.slim' + view.resume + view.end + end + + options '/error' do + raise 'error' + end end class MyTest include Nyara::Test end @@ -28,18 +49,27 @@ module Nyara describe Nyara::Test, 'integration' do before :all do configure do reset - # set :env, 'test' map '/', TestController set :root, __dir__ + set :logger, false end Nyara.setup @test = MyTest.new end + before :each do + GC.stress = false + end + + it "decodes cookie" do + @test.get "/", {"Cookie" => 'foo=bar'} + assert_equal 'bar', @test.env.cookie['foo'] + end + it "response" do @test.get "/", {'Xample' => 'résumé'} assert @test.response.success? assert_equal 'résumé', @test.request.header['Xample'] assert_equal '初めまして from test', @test.response.body @@ -62,21 +92,41 @@ @test.get "/" assert_equal '4', @test.session['b'] assert_equal '3', @test.session['a'] end - it "send file" do + it "send_file" do @test.put "/send_file/layout.erb" data = File.read Nyara.config.views_path('layout.erb') assert_equal data, @test.response.body end it "render" do @test.delete "/render" assert_include @test.response.body, "slim:edit" end + it "stream" do + @test.patch '/stream' + assert_include @test.response.body, "slim:edit" + end + + it "error" do + @test.options '/error' + assert_equal 500, @test.response.status + assert_equal false, @test.response.success? + end + + it "multipart upload" do + data = File.binread(__dir__ + '/raw_requests/multipart') + @test.env.process_request_data data + param = @test.env.request.param + assert_equal 'foo', param['foo'] + assert_equal 'bar', param['bar']['data'] + assert_equal 'baz', param['baz']['你好']['data'] + end + context "public static content" do it "found file" do @test.get "/index.html" assert_equal 200, @test.response.status assert_equal "index.html", @test.response.body @@ -94,9 +144,20 @@ end it "found but directory" do @test.get "/empty" assert_equal 404, @test.response.status + end + end + + context "before / after" do + it "invokes lifecycle callback" do + @test.get '/' + assert_nil @test.env.controller.before_invoked + assert @test.request.message_complete? + + @test.delete "/render" + assert @test.env.controller.before_invoked end end end end