Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
rackr (0.0.66)
rackr (0.0.68)
erubi (~> 1.12)
oj (~> 3.15)
rack (>= 2.0, < 4.0)
Expand Down
6 changes: 5 additions & 1 deletion examples/playground/app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,12 @@ def call(_req)
render res:
end

get 'not_string_error' do
render 2
end

get 'html_slice' do
render html: (html_slice do
render (html_slice do
div "hey", **stimulus_controller(['name', { user: 3 }])
input "hey", user: 2, **stimulus_target('name#oi')
end)
Expand Down
2 changes: 2 additions & 0 deletions lib/rackr.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

# Rackr is a simple router for Rack.
class Rackr
VERSION = '0.0.68'

class NotFound < StandardError; end

# Dump is a special error that is used to dump the content of a request.
Expand Down
24 changes: 22 additions & 2 deletions lib/rackr/action.rb
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,17 @@ def path_for(method, name)
routes.send(method)&.dig(name)
end

def render(**opts)
def render(content = nil, **opts)
if content
return RENDER[:html].call(
content.to_s,
opts[:status],
opts[:headers] || {},
opts[:charset] || 'utf-8',
content_security_policy
)
end

type = opts.keys.first
content = opts[type]

Expand Down Expand Up @@ -206,7 +216,17 @@ def render(**opts)
)
end

def build_response(**opts)
def build_response(content = nil, **opts)
if content
return BUILD_RESPONSE[:html].call(
content.to_s,
opts[:status] || 200,
opts[:headers] || {},
opts[:charset] || 'utf-8',
content_security_policy
)
end

type = opts.keys.first
content = opts[type]

Expand Down
176 changes: 111 additions & 65 deletions lib/spec/rackr/action_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,52 @@ class SomeClass2
end
subject { SomeClass.new(routes: {}, config: config) }
context 'rendering content' do
it 'defaults to html' do
result = subject.render('test')
expect(result[0]).to eq(200)
expect(result[1]).to include({ 'content-length' => '4', 'content-type' => 'text/html; charset=utf-8' })
end

context 'when receiving not string values in default param' do
it do
result = subject.render(2)
expect(result[0]).to eq(200)
expect(result[1]).to include({ 'content-length' => '1', 'content-type' => 'text/html; charset=utf-8' })
expect(result[2]).to eq(['2'])
end

it do
result = subject.render({})
expect(result[0]).to eq(200)
expect(result[1]).to include({ 'content-length' => '2', 'content-type' => 'text/html; charset=utf-8' })
expect(result[2]).to eq(['{}'])
end
end

context 'build_response' do
it 'defaults to html' do
result = subject.build_response('test').finish
expect(result[0]).to eq(200)
expect(result[1]).to include({ 'content-length' => '4', 'content-type' => 'text/html; charset=utf-8' })
end

context 'when receiving not string values in default param' do
it do
result = subject.build_response(2).finish
expect(result[0]).to eq(200)
expect(result[1]).to include({ 'content-length' => '1', 'content-type' => 'text/html; charset=utf-8' })
expect(result[2]).to eq(['2'])
end

it do
result = subject.build_response({}).finish
expect(result[0]).to eq(200)
expect(result[1]).to include({ 'content-length' => '2', 'content-type' => 'text/html; charset=utf-8' })
expect(result[2]).to eq(['{}'])
end
end
end

context 'text' do
it 'can render from string with success' do
result = subject.render(text: 'test')
Expand All @@ -38,25 +84,25 @@ class SomeClass2
result = subject.render(text: 'test', headers: { 'other' => 'header' })
expect(result[1]).to include({ 'content-length' => '4', 'content-type' => 'text/plain; charset=utf-8', 'other' => 'header' })
end
end

context 'html_response' do
it 'can render from string with success' do
response = subject.build_response(text: 'test')
expect(response.status).to eq(200)
expect(response.headers).to include({ 'content-length' => '4', 'content-type' => 'text/plain; charset=utf-8' })
end
context 'build_response text:' do
it 'can render from string with success' do
response = subject.build_response(text: 'test')
expect(response.status).to eq(200)
expect(response.headers).to include({ 'content-length' => '4', 'content-type' => 'text/plain; charset=utf-8' })
end

it 'can render text with other status' do
response = subject.build_response(text: 'test', status: 201)
expect(response.status).to eq(201)
expect(response.headers).to include({ 'content-length' => '4', 'content-type' => 'text/plain; charset=utf-8' })
end
it 'can render text with other status' do
response = subject.build_response(text: 'test', status: 201)
expect(response.status).to eq(201)
expect(response.headers).to include({ 'content-length' => '4', 'content-type' => 'text/plain; charset=utf-8' })
end

it 'can render text with other headers' do
response = subject.build_response(text: 'test', status: 201, headers: { 'other' => 'header' })
expect(response.status).to eq(201)
expect(response.headers).to include({ 'content-length' => '4', 'content-type' => 'text/plain; charset=utf-8', 'other' => 'header' })
it 'can render text with other headers' do
response = subject.build_response(text: 'test', status: 201, headers: { 'other' => 'header' })
expect(response.status).to eq(201)
expect(response.headers).to include({ 'content-length' => '4', 'content-type' => 'text/plain; charset=utf-8', 'other' => 'header' })
end
end
end

Expand All @@ -77,24 +123,24 @@ class SomeClass2
result = subject.render(html: 'test', headers: { 'other' => 'header' })
expect(result[1]).to include({ 'content-length' => '4', 'content-type' => 'text/html; charset=utf-8', 'other' => 'header' })
end
end

context 'html_response' do
it 'can render from string with success' do
response = subject.build_response(html: 'test')
expect(response.headers).to include({ 'content-length' => '4', 'content-type' => 'text/html; charset=utf-8' })
end
context 'build_response html:' do
it 'can render from string with success' do
response = subject.build_response(html: 'test')
expect(response.headers).to include({ 'content-length' => '4', 'content-type' => 'text/html; charset=utf-8' })
end

it 'can render html with other status' do
response = subject.build_response(html: 'test', status: 201)
expect(response.status).to eq(201)
expect(response.headers).to include({ 'content-length' => '4', 'content-type' => 'text/html; charset=utf-8' })
end
it 'can render html with other status' do
response = subject.build_response(html: 'test', status: 201)
expect(response.status).to eq(201)
expect(response.headers).to include({ 'content-length' => '4', 'content-type' => 'text/html; charset=utf-8' })
end

it 'can render text with other headers' do
response = subject.build_response(html: 'test', status: 201, headers: { 'other' => 'header' })
expect(response.status).to eq(201)
expect(response.headers).to include({ 'content-length' => '4', 'content-type' => 'text/html; charset=utf-8', 'other' => 'header' })
it 'can render text with other headers' do
response = subject.build_response(html: 'test', status: 201, headers: { 'other' => 'header' })
expect(response.status).to eq(201)
expect(response.headers).to include({ 'content-length' => '4', 'content-type' => 'text/html; charset=utf-8', 'other' => 'header' })
end
end
end

Expand Down Expand Up @@ -171,17 +217,17 @@ class SomeClass2
result = subject.render(view: [path, path, path], status: 404)
expect(result[1]).to include({ 'content-length' => '15', 'content-type' => 'text/html; charset=utf-8' })
end
end

context 'view_response' do
before do
allow(File).to receive(:read).and_return('file.')
end
context 'build_response view:' do
before do
allow(File).to receive(:read).and_return('file.')
end

it 'can render with success with response_instance' do
path = 'test'
response = subject.build_response(view: path)
expect(response.headers).to include({ 'content-length' => '5', 'content-type' => 'text/html; charset=utf-8' })
it 'can render with success with response_instance' do
path = 'test'
response = subject.build_response(view: path)
expect(response.headers).to include({ 'content-length' => '5', 'content-type' => 'text/html; charset=utf-8' })
end
end
end

Expand Down Expand Up @@ -216,23 +262,23 @@ class SomeClass2
result = subject.render(json: { test: 'value' }, status: 201, headers: { 'a' => 'b' })
expect(result[1]).to include({ 'a' => 'b', 'content-length' => '16', 'content-type' => 'application/json; charset=utf-8' })
end
end

context 'json_response' do
it 'can render from hash with success' do
response = subject.build_response(json: { test: 'value' })
expect(response.headers).to include({ 'content-length' => '16', 'content-type' => 'application/json; charset=utf-8' })
end
context 'build_response json:' do
it 'can render from hash with success' do
response = subject.build_response(json: { test: 'value' })
expect(response.headers).to include({ 'content-length' => '16', 'content-type' => 'application/json; charset=utf-8' })
end

it 'can render json with other status' do
response = subject.build_response(json: { test: 'value' }, status: 201)
expect(response.status).to eq(201)
expect(response.headers).to include({ 'content-length' => '16', 'content-type' => 'application/json; charset=utf-8' })
end
it 'can render json with other status' do
response = subject.build_response(json: { test: 'value' }, status: 201)
expect(response.status).to eq(201)
expect(response.headers).to include({ 'content-length' => '16', 'content-type' => 'application/json; charset=utf-8' })
end

it 'can render text with other headers' do
response = subject.build_response(json: { test: 'value' }, headers: { 'a' => 'b' })
expect(response.headers).to include({ 'content-length' => '16', 'content-type' => 'application/json; charset=utf-8', 'a' => 'b' })
it 'can render text with other headers' do
response = subject.build_response(json: { test: 'value' }, headers: { 'a' => 'b' })
expect(response.headers).to include({ 'content-length' => '16', 'content-type' => 'application/json; charset=utf-8', 'a' => 'b' })
end
end
end
end
Expand Down Expand Up @@ -269,19 +315,19 @@ class SomeClass2
result = subject.head(404, headers: { 'some' => 'header' })
expect(result[1]).to include({ 'some' => 'header' })
end
end

context 'head_response' do
it 'return bare status code' do
response = subject.build_response(head: 404)
expect(response).to be_a(Rack::Response)
expect(response.status).to eq(404)
end
context 'build_response head:' do
it 'return bare status code' do
response = subject.build_response(head: 404)
expect(response).to be_a(Rack::Response)
expect(response.status).to eq(404)
end

it 'can receive headers as named params' do
response = subject.build_response(head: 404, headers: { 'some' => 'header' })
expect(response).to be_a(Rack::Response)
expect(response.headers).to include({ 'some' => 'header' })
it 'can receive headers as named params' do
response = subject.build_response(head: 404, headers: { 'some' => 'header' })
expect(response).to be_a(Rack::Response)
expect(response.headers).to include({ 'some' => 'header' })
end
end
end

Expand Down
2 changes: 1 addition & 1 deletion perf_tests/r10k/Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ end)

g.theme_greyscale
g.colors = [
'#990000',
'#5b1f31',
'#383838',
'#989898',
'#c8c8c8',
Expand Down
20 changes: 10 additions & 10 deletions perf_tests/r10k/apps/rackr_1_10.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,33 @@
require_relative '../../../lib/rackr'
App = Rackr.new.call do
get '/a/:a' do |req|
render html: "4797-#{req.params[:a]}"
render "4797-#{req.params[:a]}"
end
get '/b/:a' do |req|
render html: "4798-#{req.params[:a]}"
render "4798-#{req.params[:a]}"
end
get '/c/:a' do |req|
render html: "4799-#{req.params[:a]}"
render "4799-#{req.params[:a]}"
end
get '/d/:a' do |req|
render html: "47100-#{req.params[:a]}"
render "47100-#{req.params[:a]}"
end
get '/e/:a' do |req|
render html: "47101-#{req.params[:a]}"
render "47101-#{req.params[:a]}"
end
get '/f/:a' do |req|
render html: "47102-#{req.params[:a]}"
render "47102-#{req.params[:a]}"
end
get '/g/:a' do |req|
render html: "47103-#{req.params[:a]}"
render "47103-#{req.params[:a]}"
end
get '/h/:a' do |req|
render html: "47104-#{req.params[:a]}"
render "47104-#{req.params[:a]}"
end
get '/i/:a' do |req|
render html: "47105-#{req.params[:a]}"
render "47105-#{req.params[:a]}"
end
get '/j/:a' do |req|
render html: "47106-#{req.params[:a]}"
render "47106-#{req.params[:a]}"
end
end
Loading