From 950b28835322bc69cd4a07cd839512a4a5aca913 Mon Sep 17 00:00:00 2001 From: Gabriel Naiman Date: Wed, 7 Oct 2020 14:22:42 -0300 Subject: [PATCH] Added render data --- lib/rasti/web/render.rb | 6 ++++++ spec/render_spec.rb | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+) diff --git a/lib/rasti/web/render.rb b/lib/rasti/web/render.rb index df02668..0274898 100644 --- a/lib/rasti/web/render.rb +++ b/lib/rasti/web/render.rb @@ -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) diff --git a/spec/render_spec.rb b/spec/render_spec.rb index 9940533..506b7e2 100644 --- a/spec/render_spec.rb +++ b/spec/render_spec.rb @@ -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'