diff --git a/lennarb.gemspec b/lennarb.gemspec index 5a7b7dd..4032675 100644 --- a/lennarb.gemspec +++ b/lennarb.gemspec @@ -6,7 +6,9 @@ Gem::Specification.new do |spec| spec.name = 'lennarb' spec.version = Lennarb::VERSION - spec.summary = 'A lightweight and experimental web framework for Ruby.' + spec.summary = <<~DESC + Lennarb provides a lightweight yet robust solution for web routing in Ruby, focusing on performance and simplicity. + DESC spec.authors = ['Aristóteles Coutinho'] spec.license = 'MIT' diff --git a/lib/lennarb/request.rb b/lib/lennarb/request.rb index b1e13f9..6dcea53 100644 --- a/lib/lennarb/request.rb +++ b/lib/lennarb/request.rb @@ -20,13 +20,20 @@ def initialize(env, route_params = {}) # Get the request body # # @returns [String] + # def params @params ||= super.merge(@route_params) end + # Read the body of the request + # + # @returns [String] + # + def body = @body ||= super.read + private - # Get the query string + # Get the query string parameters # # @returns [String] # diff --git a/lib/lennarb/version.rb b/lib/lennarb/version.rb index 0bb447c..6e422ea 100644 --- a/lib/lennarb/version.rb +++ b/lib/lennarb/version.rb @@ -4,7 +4,7 @@ # Copyright, 2023-2024, by Aristóteles Coutinho. class Lennarb - VERSION = '1.0.1' + VERSION = '1.1.0' public_constant :VERSION end diff --git a/test/lib/lennarb/application/test_base.rb b/test/lib/lennarb/application/test_base.rb index 73737f7..50cc4f2 100644 --- a/test/lib/lennarb/application/test_base.rb +++ b/test/lib/lennarb/application/test_base.rb @@ -4,6 +4,7 @@ # Copyright, 2023-2024, by Aristóteles Coutinho. require 'test_helper' +require 'json' class Lennarb module Application @@ -50,6 +51,18 @@ class MyApp < Lennarb::Application::Base res.html('POST Response') end + post '/json' do |req, res| + begin + res.status = 201 + JSON.parse(req.body) + res.json(req.body) + rescue JSON::ParserError + res.status = 500 + result = { error: 'Invalid JSON body' }.to_json + res.json(result) + end + end + get '/plugin' do |_req, res| res.status = 200 res.html(test_plugin_method) @@ -72,6 +85,28 @@ def test_post assert_equal 'POST Response', last_response.body end + def test_post_with_valid_json_body + json_body = '{"key":"value"}' + headers = { 'CONTENT_TYPE' => 'application/json' } + + post '/json', json_body, headers + + assert_equal 201, last_response.status + body = JSON.parse(last_response.body) + assert_equal({ "key" => "value" }, body) + end + + def test_post_with_invalid_json_body + json_body = '{"key":"value' + + headers = { 'CONTENT_TYPE' => 'application/json' } + + post '/json', json_body, headers + + assert_equal 500, last_response.status + assert_equal({ "error" => "Invalid JSON body" }, JSON.parse(last_response.body)) + end + def test_before_hooks get '/' diff --git a/test/lib/lennarb/test_request.rb b/test/lib/lennarb/test_request.rb index 0e12884..064c2ea 100644 --- a/test/lib/lennarb/test_request.rb +++ b/test/lib/lennarb/test_request.rb @@ -46,13 +46,7 @@ def test_content_length def test_body request = Lennarb::Request.new({ 'rack.input' => StringIO.new('foo') }) - assert_equal('foo', request.body.read) - end - - def test_body_with_empty_body - request = Lennarb::Request.new({}) - - assert_nil(request.body) + assert_equal('foo', request.body) end end end