Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add HTTP status codes generator #1821

Merged
merged 9 commits into from
Jun 18, 2020
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
11 changes: 11 additions & 0 deletions doc/internet/http.md
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
48 changes: 48 additions & 0 deletions lib/faker/default/internet_http.rb
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
psibi marked this conversation as resolved.
Show resolved Hide resolved

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)
Zeragamba marked this conversation as resolved.
Show resolved Hide resolved
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
40 changes: 40 additions & 0 deletions test/faker/default/test_faker_internet_http.rb
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