forked from postrank-labs/goliath
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
force the resposne format so Rack::ContentLength will work correctly
- Loading branch information
Showing
6 changed files
with
80 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
#!/usr/bin/env ruby | ||
$:<< '../lib' << 'lib' | ||
|
||
require 'goliath' | ||
|
||
class Valid < Goliath::API | ||
|
||
# reload code on every request in dev environment | ||
use ::Rack::Reloader, 0 if Goliath.dev? | ||
|
||
use Goliath::Rack::Params | ||
use Goliath::Rack::ValidationError | ||
|
||
use Goliath::Rack::Validation::RequiredParam, {:key => 'test'} | ||
|
||
def response(env) | ||
[200, {}, 'OK'] | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
module Goliath | ||
module Rack | ||
class DefaultResponseFormat | ||
def initialize(app) | ||
@app = app | ||
end | ||
|
||
def call(env) | ||
async_cb = env['async.callback'] | ||
env['async.callback'] = Proc.new do |status, headers, body| | ||
async_cb.call(post_process(status, headers, body)) | ||
end | ||
|
||
status, headers, body = @app.call(env) | ||
post_process(status, headers, body) | ||
end | ||
|
||
def post_process(status, headers, body) | ||
return [status, headers, body] if body.respond_to?(:to_ary) | ||
|
||
new_body = [] | ||
if body.respond_to?(:each) | ||
body.each { |chunk| new_body << chunk } | ||
else | ||
new_body << body | ||
end | ||
new_body.collect! { |item| item.to_s } | ||
|
||
[status, headers, new_body.flatten] | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,4 +35,4 @@ def call(env) | |
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
require 'spec_helper' | ||
require File.join(File.dirname(__FILE__), '../../', 'examples/valid') | ||
|
||
describe Valid do | ||
include Goliath::TestHelper | ||
|
||
let(:err) { Proc.new { fail "API request failed" } } | ||
|
||
it 'returns OK with param' do | ||
with_api(Valid) do | ||
get_request({:query => {:test => 'test'}}, err) do |c| | ||
c.response.should == 'OK' | ||
end | ||
end | ||
end | ||
|
||
it 'returns error without param' do | ||
with_api(Valid) do | ||
get_request({}, err) do |c| | ||
c.response.should == '[:error, "Test identifier missing"]' | ||
end | ||
end | ||
end | ||
end |