Skip to content

Commit

Permalink
Added render data
Browse files Browse the repository at this point in the history
  • Loading branch information
Gabriel Naiman committed Oct 7, 2020
1 parent ed208a3 commit 950b288
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
6 changes: 6 additions & 0 deletions lib/rasti/web/render.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ def file(filename, *args)
File.read(filename)
end

def data(content, *args)
respond_with extract_status(args),
extract_headers(args),
content
end

def partial(template, locals={})
response.headers.merge! Headers.for_html
response.write view_context.render(template, locals)
Expand Down
40 changes: 40 additions & 0 deletions spec/render_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,46 @@

end

describe 'Data' do

let(:content) { 'Response data' }

it 'Body' do
render.data content

response.status.must_equal 200
response['Content-Type'].must_be_nil
response.body.must_equal [content]
end

it 'Body and status' do
render.data content, 206

response.status.must_equal 206
response['Content-Type'].must_be_nil
response.body.must_equal [content]
end

it 'Body and headers' do
render.data content, Rasti::Web::Headers.for_file('test_file.txt')

response.status.must_equal 200
response['Content-Type'].must_equal 'text/plain; charset=utf-8'
response['Content-Disposition'].must_equal 'attachment; filename="test_file.txt"'
response.body.must_equal [content]
end

it 'Body, status and headers' do
render.data content, 206, Rasti::Web::Headers.for_file('test_file.txt')

response.status.must_equal 206
response['Content-Type'].must_equal 'text/plain; charset=utf-8'
response['Content-Disposition'].must_equal 'attachment; filename="test_file.txt"'
response.body.must_equal [content]
end

end

it 'Partial' do
render.partial 'context_and_locals', title: 'Welcome', text: 'Hello world'

Expand Down

0 comments on commit 950b288

Please sign in to comment.