Skip to content

Commit

Permalink
Merge pull request #64 from aristotelesbr/development
Browse files Browse the repository at this point in the history
docs: update gem description
  • Loading branch information
aristotelesbr authored Oct 7, 2024
2 parents dfc6ffd + ad42670 commit a4fd7c6
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 10 deletions.
4 changes: 3 additions & 1 deletion lennarb.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down
9 changes: 8 additions & 1 deletion lib/lennarb/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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]
#
Expand Down
2 changes: 1 addition & 1 deletion lib/lennarb/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
35 changes: 35 additions & 0 deletions test/lib/lennarb/application/test_base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
# Copyright, 2023-2024, by Aristóteles Coutinho.

require 'test_helper'
require 'json'

class Lennarb
module Application
Expand Down Expand Up @@ -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)
Expand All @@ -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 '/'

Expand Down
8 changes: 1 addition & 7 deletions test/lib/lennarb/test_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit a4fd7c6

Please sign in to comment.