Created
December 25, 2019 22:20
-
-
Save simi/f67a44399d816749c25f63111812ccff to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ConditionRouter | |
def self.call(env) | |
if env["REQUEST_METHOD"] == "GET" | |
if env["QUERY_STRING"] == "" | |
[200, {}, ["empty query string"]] | |
else | |
[200, {}, ["query string: #{env["QUERY_STRING"]}"]] | |
end | |
else | |
if env["REQUEST_METHOD"].nil? | |
[400, {}, ["no request method provided"]] | |
else | |
[405, {}, ["unsupported method: #{env["REQUEST_METHOD"]}"]] | |
end | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require_relative 'condition_router' | |
require_relative 'router_test' | |
require 'minitest' | |
require 'minitest/autorun' | |
class ConditionRouterTest < Minitest::Test | |
include RouterTest | |
def app | |
ConditionRouter | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
source 'https://rubygems.org' | |
ruby '2.7.0' | |
gem 'rack' | |
gem 'rack-test' | |
gem 'minitest' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
GEM | |
remote: https://rubygems.org/ | |
specs: | |
minitest (5.13.0) | |
rack (2.0.8) | |
rack-test (1.1.0) | |
rack (>= 1.0, < 3) | |
PLATFORMS | |
ruby | |
DEPENDENCIES | |
minitest | |
rack | |
rack-test | |
RUBY VERSION | |
ruby 2.7.0p0 | |
BUNDLED WITH | |
2.1.2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class PatternRouter | |
def self.call(env) | |
# pattern matching works only for symbols now | |
# we need to symbolize keys first | |
symbolized_env = env.inject({}){|memo,(k,v)| memo[k.to_sym] = v; memo} | |
case symbolized_env | |
in {REQUEST_METHOD: "GET", QUERY_STRING: ""} | |
[200, {}, ["empty query string"]] | |
in {REQUEST_METHOD: "GET", QUERY_STRING: params} | |
[200, {}, ["query string: #{params}"]] | |
in {REQUEST_METHOD: method} | |
[405, {}, ["unsupported method: #{method}"]] | |
else | |
[400, {}, ["no request method provided"]] | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require_relative 'pattern_router' | |
require_relative 'router_test' | |
require 'minitest' | |
require 'minitest/autorun' | |
class PatternRouterTest < Minitest::Test | |
include RouterTest | |
def app | |
PatternRouter | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rack' | |
require 'rack/test' | |
module RouterTest | |
include Rack::Test::Methods | |
def test_get_without_args | |
get '/' | |
assert last_response.ok? | |
assert_equal last_response.body, "empty query string" | |
end | |
def test_get_with_query_string | |
get '/?test' | |
assert last_response.ok? | |
assert_equal last_response.body, "query string: test" | |
end | |
def test_non_get_request | |
post '/' | |
assert last_response.method_not_allowed? | |
assert_equal last_response.body, "unsupported method: POST" | |
end | |
def test_no_method | |
# mock empty env hash | |
env = {} | |
last_request = Rack::Request.new(env) | |
status, headers, body = app.call(last_request.env) | |
last_response = Rack::MockResponse.new(status, headers, body) | |
assert last_response.bad_request? | |
assert_equal last_response.body, "no request method provided" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment