-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add HTTP status codes generator (#1821)
* Add HTTP status codes generator * Join methods in a single one * Add http doc * Fix method name in the docs * Add YARD docs * Change "@faker.version" to "next" * Fix rubocop offense * Update version to "next" in markdown file * Remove redundant if statement Co-authored-by: Koichi ITO <[email protected]> Co-authored-by: Vitor Oliveira <[email protected]> Co-authored-by: Koichi ITO <[email protected]>
- Loading branch information
1 parent
9b0c369
commit 2242a97
Showing
3 changed files
with
99 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# Faker::Internet::HTTP | ||
|
||
Available since version next. | ||
|
||
# Keyword arguments: group | ||
Faker::Internet::HTTP.status_code #=> 418 | ||
Faker::Internet::HTTP.status_code(group: :information) #=> 102 | ||
Faker::Internet::HTTP.status_code(group: :successful) #=> 200 | ||
Faker::Internet::HTTP.status_code(group: :redirect) #=> 306 | ||
Faker::Internet::HTTP.status_code(group: :client_error) #=> 451 | ||
Faker::Internet::HTTP.status_code(group: :server_error) #=> 502 |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
# frozen_string_literal: true | ||
|
||
module Faker | ||
class Internet | ||
class HTTP < Base | ||
STATUS_CODES = { | ||
information: [100, 101, 102, 103], | ||
successful: [200, 201, 202, 203, 204, 205, 206, 207, 208, 226], | ||
redirect: [300, 301, 302, 303, 304, 305, 306, 307, 308], | ||
client_error: [400, 401, 402, 403, 404, 405, 406, 407, 408, 409, 410, 411, 412, | ||
413, 414, 415, 416, 417, 418, 421, 422, 423, 424, 425, 426, 428, | ||
429, 431, 451], | ||
server_error: [500, 501, 502, 503, 504, 505, 506, 507, 508, 510, 511] | ||
}.freeze | ||
|
||
STATUS_CODES_GROUPS = STATUS_CODES.keys.freeze | ||
|
||
class << self | ||
## | ||
# Produces an HTTP status code | ||
# | ||
# @return [Integer] | ||
# | ||
# @example | ||
# Faker::Internet::HTTP.status_code #=> 418 | ||
# @example | ||
# Faker::Internet::HTTP.status_code(group: :information) #=> 102 | ||
# @example | ||
# Faker::Internet::HTTP.status_code(group: :successful) #=> 200 | ||
# @example | ||
# Faker::Internet::HTTP.status_code(group: :redirect) #=> 306 | ||
# @example | ||
# Faker::Internet::HTTP.status_code(group: :client_error) #=> 451 | ||
# @example | ||
# Faker::Internet::HTTP.status_code(group: :server_error) #=> 502 | ||
# | ||
# @faker.version next | ||
def status_code(group: nil) | ||
return STATUS_CODES[STATUS_CODES_GROUPS.sample].sample unless group | ||
|
||
raise ArgumentError, 'Invalid HTTP status code group' unless STATUS_CODES_GROUPS.include?(group) | ||
|
||
STATUS_CODES[group].sample | ||
end | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# frozen_string_literal: true | ||
|
||
require_relative '../../test_helper' | ||
|
||
class TestFakerInternetHTTP < Test::Unit::TestCase | ||
def setup | ||
@tester = Faker::Internet::HTTP | ||
end | ||
|
||
def test_status_code | ||
assert @tester.status_code.to_s.match(/^[1-5]\d{2}$/) | ||
end | ||
|
||
def test_information_status_code | ||
assert @tester.status_code(group: :information).to_s.match(/^1\d{2}$/) | ||
end | ||
|
||
def test_successful_status_code | ||
assert @tester.status_code(group: :successful).to_s.match(/^2\d{2}$/) | ||
end | ||
|
||
def test_redirect_status_code | ||
assert @tester.status_code(group: :redirect).to_s.match(/^3\d{2}$/) | ||
end | ||
|
||
def test_client_error_status_code | ||
assert @tester.status_code(group: :client_error).to_s.match(/^4\d{2}$/) | ||
end | ||
|
||
def test_server_error_status_code | ||
assert @tester.status_code(group: :server_error).to_s.match(/^5\d{2}$/) | ||
end | ||
|
||
def test_invalid_http_status_code_group | ||
exception = assert_raises ArgumentError do | ||
@tester.status_code(group: :inexistent) | ||
end | ||
assert_equal('Invalid HTTP status code group', exception.message) | ||
end | ||
end |