test/angelo_spec.rb in angelo-0.4.1 vs test/angelo_spec.rb in angelo-0.5.0

- old
+ new

@@ -21,10 +21,14 @@ get '/redirect' do redirect '/' end + get '/redirect_forever' do + redirect! '/' + end + get '/wait' do sleep 3 nil end @@ -47,42 +51,48 @@ last_response_must_be_json obj end it 'redirects' do get '/redirect' + last_response.status.must_equal 302 + last_response.headers['Location'].must_equal '/' + end + + it 'redirects permanently' do + get '/redirect_forever' last_response.status.must_equal 301 last_response.headers['Location'].must_equal '/' end it 'responds to requests concurrently' do wait_end = nil get_end = nil latch = CountDownLatch.new 2 - ActorPool.define_action :do_wait do + Actor.define_action :do_wait do get '/wait' wait_end = Time.now latch.count_down end - ActorPool.define_action :do_get do + Actor.define_action :do_get do sleep 1 get '/' get_end = Time.now latch.count_down end - ActorPool.unstop! - $pool.async :do_wait - $pool.async :do_get + Actor.unstop! + $pool[0].async :do_wait + $pool[1].async :do_get latch.wait get_end.must_be :<, wait_end - ActorPool.stop! - ActorPool.remove_action :do_wait - ActorPool.remove_action :do_get + Actor.stop! + Actor.remove_action :do_wait + Actor.remove_action :do_get end it 'does not crash when receiving unknown http request type' do r = HTTP.patch(url('/')) assert @server.alive? @@ -316,10 +326,15 @@ content_type :json params end end + post '/json_array' do + content_type :json + {params: params, body: request_body} + end + end it 'parses formencoded body when content-type is formencoded' do post '/json', obj, {'Content-Type' => Angelo::FORM_TYPE} last_response_must_be_json obj_s @@ -347,10 +362,23 @@ send m, '/json?foo' last_response_must_be_json('foo' => nil) end end + it 'parses JSON array bodies but does not merge into params' do + post '/json_array?foo=bar', [123,234].to_json, {'Content-Type' => Angelo::JSON_TYPE} + last_response_must_be_json({ + "params" => {"foo" => "bar"}, + "body" => [123,234] + }) + end + + it 'does not die on malformed JSON' do + post '/json_array?foo=bar', '{]sdfj2if08yth4j]j,:jsd;f; function()', {'Content-Type' => Angelo::JSON_TYPE} + last_response.status.must_equal 400 + end + end describe 'request_headers helper' do define_app do @@ -549,9 +577,23 @@ end it 'sets dir for public files' do get '/' last_response_must_be_html File.join(@server.base.root, 'sucka') + end + + end + + describe 'default_headers' do + + define_app do + default_headers "Access-Control-Allow-Origin" => "*" + get('/'){ 'hi' } + end + + it 'adds a default headers' do + get '/' + last_response.headers['Access-Control-Allow-Origin'].must_equal "*" end end end