From 89d78c1ec6d95c9cc56d8463a0a1cda2ff16ca68 Mon Sep 17 00:00:00 2001 From: John Bond Date: Tue, 6 Jul 2021 15:55:29 +0200 Subject: [PATCH] Stdlib::Http::Method: Add new type for http methods This PR creates new new resources: * Stdlib::Http::Method for validating http methods * Stdlib::Http::Status This is just a copy of Stdlib::Httpstatus * make Stdlib::Httpstatus and alias to Stdlib::Http::Status Ideally we would deprecate Stdlib::Httpstatus in favour of Stdlib::Http::Status Co-authored-by: Ewoud Kohl van Wijngaarden --- REFERENCE.md | 25 ++++++++++++++- spec/type_aliases/http__method_spec.rb | 40 ++++++++++++++++++++++++ spec/type_aliases/http__status_spec.rb | 38 +++++++++++++++++++++++ types/http/method.pp | 43 ++++++++++++++++++++++++++ types/http/status.pp | 3 ++ types/httpstatus.pp | 4 ++- 6 files changed, 151 insertions(+), 2 deletions(-) create mode 100644 spec/type_aliases/http__method_spec.rb create mode 100644 spec/type_aliases/http__status_spec.rb create mode 100644 types/http/method.pp create mode 100644 types/http/status.pp diff --git a/REFERENCE.md b/REFERENCE.md index 4df7f5d13..c6f9b3f2e 100644 --- a/REFERENCE.md +++ b/REFERENCE.md @@ -289,6 +289,8 @@ OpenSSL. * [`Stdlib::HTTPSUrl`](#Stdlib--HTTPSUrl): Validate a HTTPS URL * [`Stdlib::HTTPUrl`](#Stdlib--HTTPUrl): Validate a HTTP(S) URL * [`Stdlib::Host`](#Stdlib--Host): Validate a host (FQDN or IP address) +* [`Stdlib::Http::Method`](#Stdlib--Http--Method): Valid HTTP method verbs +* [`Stdlib::Http::Status`](#Stdlib--Http--Status): A valid HTTP status code per RFC9110 * [`Stdlib::HttpStatus`](#Stdlib--HttpStatus): Validate a HTTP status code * [`Stdlib::IP::Address`](#Stdlib--IP--Address): Validate an IP address * [`Stdlib::IP::Address::Nosubnet`](#Stdlib--IP--Address--Nosubnet): Validate an IP address without subnet @@ -7796,11 +7798,32 @@ Validate a host (FQDN or IP address) Alias of `Variant[Stdlib::Fqdn, Stdlib::Compat::Ip_address]` +### `Stdlib::Http::Method` + +Valid HTTP method verbs + +* **See also** + * https://www.iana.org/assignments/http-methods/http-methods.xhtml + +Alias of `Enum['ACL', 'BASELINE-CONTROL', 'BIND', 'CHECKIN', 'CHECKOUT', 'CONNECT', 'COPY', 'DELETE', 'GET', 'HEAD', 'LABEL', 'LINK', 'LOCK', 'MERGE', 'MKACTIVITY', 'MKCALENDAR', 'MKCOL', 'MKREDIRECTREF', 'MKWORKSPACE', 'MOVE', 'OPTIONS', 'ORDERPATCH', 'PATCH', 'POST', 'PRI', 'PROPFIND', 'PROPPATCH', 'PUT', 'REBIND', 'REPORT', 'SEARCH', 'TRACE', 'UNBIND', 'UNCHECKOUT', 'UNLINK', 'UNLOCK', 'UPDATE', 'UPDATEREDIRECTREF', 'VERSION-CONTROL']` + +### `Stdlib::Http::Status` + +A valid HTTP status code per RFC9110 + +* **See also** + * https://httpwg.org/specs/rfc9110.html#overview.of.status.codes + +Alias of `Integer[100, 599]` + ### `Stdlib::HttpStatus` Validate a HTTP status code -Alias of `Integer[100, 599]` +* **See also** + * Stdlib::Http::Status + +Alias of `Stdlib::Http::Status` ### `Stdlib::IP::Address` diff --git a/spec/type_aliases/http__method_spec.rb b/spec/type_aliases/http__method_spec.rb new file mode 100644 index 000000000..e80d02027 --- /dev/null +++ b/spec/type_aliases/http__method_spec.rb @@ -0,0 +1,40 @@ +require 'spec_helper' + +describe 'Stdlib::Http::Method' do + describe 'valid HTTP Methods' do + [ + 'HEAD', + 'GET', + 'PUT', + 'DELETE', + 'TRACE', + ].each do |value| + describe value.inspect do + it { is_expected.to allow_value(value) } + end + end + end + + describe 'invalid path handling' do + context 'garbage inputs' do + [ + nil, + [nil], + [nil, nil], + { 'foo' => 'bar' }, + {}, + '', + 'https', + '199', + 600, + 1_000, + 'Ok', + 'get', + ].each do |value| + describe value.inspect do + it { is_expected.not_to allow_value(value) } + end + end + end + end +end diff --git a/spec/type_aliases/http__status_spec.rb b/spec/type_aliases/http__status_spec.rb new file mode 100644 index 000000000..123612fc3 --- /dev/null +++ b/spec/type_aliases/http__status_spec.rb @@ -0,0 +1,38 @@ +require 'spec_helper' + +describe 'Stdlib::Http::Status' do + describe 'valid HTTP Status' do + [ + 200, + 302, + 404, + 418, + 503, + ].each do |value| + describe value.inspect do + it { is_expected.to allow_value(value) } + end + end + end + + describe 'invalid path handling' do + context 'garbage inputs' do + [ + nil, + [nil], + [nil, nil], + { 'foo' => 'bar' }, + {}, + '', + 'https', + '199', + 600, + 1_000, + ].each do |value| + describe value.inspect do + it { is_expected.not_to allow_value(value) } + end + end + end + end +end diff --git a/types/http/method.pp b/types/http/method.pp new file mode 100644 index 000000000..3b50ff0b8 --- /dev/null +++ b/types/http/method.pp @@ -0,0 +1,43 @@ +# @summary Valid HTTP method verbs +# @see https://www.iana.org/assignments/http-methods/http-methods.xhtml +type Stdlib::Http::Method = Enum[ + 'ACL', + 'BASELINE-CONTROL', + 'BIND', + 'CHECKIN', + 'CHECKOUT', + 'CONNECT', + 'COPY', + 'DELETE', + 'GET', + 'HEAD', + 'LABEL', + 'LINK', + 'LOCK', + 'MERGE', + 'MKACTIVITY', + 'MKCALENDAR', + 'MKCOL', + 'MKREDIRECTREF', + 'MKWORKSPACE', + 'MOVE', + 'OPTIONS', + 'ORDERPATCH', + 'PATCH', + 'POST', + 'PRI', + 'PROPFIND', + 'PROPPATCH', + 'PUT', + 'REBIND', + 'REPORT', + 'SEARCH', + 'TRACE', + 'UNBIND', + 'UNCHECKOUT', + 'UNLINK', + 'UNLOCK', + 'UPDATE', + 'UPDATEREDIRECTREF', + 'VERSION-CONTROL', +] diff --git a/types/http/status.pp b/types/http/status.pp new file mode 100644 index 000000000..08a23fdc7 --- /dev/null +++ b/types/http/status.pp @@ -0,0 +1,3 @@ +# @summary A valid HTTP status code per RFC9110 +# @see https://httpwg.org/specs/rfc9110.html#overview.of.status.codes +type Stdlib::Http::Status = Integer[100, 599] diff --git a/types/httpstatus.pp b/types/httpstatus.pp index 4199d8acf..1a73221eb 100644 --- a/types/httpstatus.pp +++ b/types/httpstatus.pp @@ -1,2 +1,4 @@ # @summary Validate a HTTP status code -type Stdlib::HttpStatus = Integer[100, 599] +# @deprecated Use Stdlib::Http::Status +# @see Stdlib::Http::Status +type Stdlib::HttpStatus = Stdlib::Http::Status