Skip to content

Commit 09945cd

Browse files
authored
Merge pull request #7 from dnnrly/feature/methods
Feature/methods
2 parents 405ad8d + 0119b01 commit 09945cd

5 files changed

Lines changed: 209 additions & 91 deletions

File tree

cmd/headers.go

Lines changed: 0 additions & 46 deletions
This file was deleted.

cmd/root.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,17 +42,31 @@ func Execute() {
4242

4343
func init() {
4444
rootCmd.PersistentFlags().BoolVarP(&titles, "titles", "", titles, "List titles of the summaries available")
45+
rootCmd.AddCommand(subCmd("methods", "method", httpref.Methods))
46+
rootCmd.AddCommand(subCmd("statuses", "status", httpref.Statuses))
47+
rootCmd.AddCommand(subCmd("headers", "header", httpref.Headers))
48+
}
49+
50+
func subCmd(name, alias string, ref httpref.References) *cobra.Command {
51+
return &cobra.Command{
52+
Use: fmt.Sprintf("%s [filter]", name),
53+
Aliases: []string{alias},
54+
Short: fmt.Sprintf("References for common HTTP %s", name),
55+
Run: referenceCmd(ref),
56+
}
4557
}
4658

4759
func root(cmd *cobra.Command, args []string) error {
4860
results := append(httpref.Statuses.Titles(), httpref.Headers.Titles()...)
61+
results = append(results, httpref.Methods.Titles()...)
4962

5063
if !titles {
5164
if len(args) == 0 {
5265
fmt.Fprintf(os.Stderr, "Must specify something to filter by\n")
5366
os.Exit(1)
5467
} else {
5568
results = append(httpref.Statuses, httpref.Headers...)
69+
results = append(results, httpref.Methods...)
5670
results = results.ByName(args[0])
5771
}
5872
}
@@ -75,3 +89,17 @@ func printResults(results httpref.References) {
7589
}
7690
}
7791
}
92+
93+
func referenceCmd(ref httpref.References) func(cmd *cobra.Command, args []string) {
94+
return func(cmd *cobra.Command, args []string) {
95+
results := ref
96+
97+
if len(args) == 0 {
98+
results = results.Titles()
99+
} else {
100+
results = results.ByName(args[0])
101+
}
102+
103+
printResults(results)
104+
}
105+
}

cmd/status.go

Lines changed: 0 additions & 45 deletions
This file was deleted.

methods.go

Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
package httpref
2+
3+
var Methods = References{
4+
{
5+
Name: "Methods",
6+
IsTitle: true,
7+
Summary: "https://developer.mozilla.org/en-US/docs/Web/HTTP/MEthods",
8+
Description: `HTTP defines a set of request methods to indicate the desired action to be performed for a given resource. Although they can also be nouns, these request methods are sometimes referred as HTTP verbs. Each of them implements a different semantic, but some common features are shared by a group of them: e.g. a request method can be safe, idempotent, or cacheable.
9+
10+
https://developer.mozilla.org/en-US/docs/Web/HTTP/MEthods`,
11+
},
12+
{
13+
Name: "GET",
14+
Summary: "The GET method requests a representation of the specified resource. Requests using GET should only retrieve data.",
15+
Description: `The HTTP GET method requests a representation of the specified resource. Requests using GET should only retrieve data.
16+
Request has body No
17+
Successful response has body Yes
18+
Safe Yes
19+
Idempotent Yes
20+
Cacheable Yes
21+
Allowed in HTML forms Yes
22+
23+
https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/GET`,
24+
},
25+
{
26+
Name: "HEAD",
27+
Summary: "The HEAD method asks for a response identical to that of a GET request, but without the response body.",
28+
Description: `The HTTP HEAD method requests the headers that are returned if the specified resource would be requested with an HTTP GET method. Such a request can be done before deciding to download a large resource to save bandwidth, for example.
29+
30+
A response to a HEAD method should not have a body. If so, it must be ignored. Even so, entity headers describing the content of the body, like Content-Length may be included in the response. They don't relate to the body of the HEAD response, which should be empty, but to the body of similar request using the GET method would have returned as a response.
31+
32+
If the result of a HEAD request shows that a cached resource after a GET request is now outdated, the cache is invalidated, even if no GET request has been made.
33+
Request has body No
34+
Successful response has body No
35+
Safe Yes
36+
Idempotent Yes
37+
Cacheable Yes
38+
Allowed in HTML forms No
39+
40+
https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/HEAD`,
41+
},
42+
{
43+
Name: "POST",
44+
Summary: "The POST method is used to submit an entity to the specified resource, often causing a change in state or side effects on the server.",
45+
Description: `The HTTP POST method sends data to the server. The type of the body of the request is indicated by the Content-Type header.
46+
47+
The difference between PUT and POST is that PUT is idempotent: calling it once or several times successively has the same effect (that is no side effect), where successive identical POST may have additional effects, like passing an order several times.
48+
49+
A POST request is typically sent via an HTML form and results in a change on the server. In this case, the content type is selected by putting the adequate string in the enctype attribute of the <form> element or the formenctype attribute of the <input> or <button> elements:
50+
51+
application/x-www-form-urlencoded: the keys and values are encoded in key-value tuples separated by '&', with a '=' between the key and the value. Non-alphanumeric characters in both keys and values are percent encoded: this is the reason why this type is not suitable to use with binary data (use multipart/form-data instead)
52+
multipart/form-data: each value is sent as a block of data ("body part"), with a user agent-defined delimiter ("boundary") separating each part. The keys are given in the Content-Disposition header of each part.
53+
text/plain
54+
55+
When the POST request is sent via a method other than an HTML form — like via an XMLHttpRequest — the body can take any type. As described in the HTTP 1.1 specification, POST is designed to allow a uniform method to cover the following functions:
56+
57+
Annotation of existing resources
58+
Posting a message to a bulletin board, newsgroup, mailing list, or similar group of articles;
59+
Adding a new user through a signup modal;
60+
Providing a block of data, such as the result of submitting a form, to a data-handling process;
61+
Extending a database through an append operation.
62+
63+
Request has body Yes
64+
Successful response has body Yes
65+
Safe No
66+
Idempotent No
67+
Cacheable Only if freshness information is included
68+
Allowed in HTML forms Yes
69+
70+
https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/POST`,
71+
},
72+
{
73+
Name: "PUT",
74+
Summary: "The PUT method replaces all current representations of the target resource with the request payload.",
75+
Description: `The HTTP PUT request method creates a new resource or replaces a representation of the target resource with the request payload.
76+
77+
The difference between PUT and POST is that PUT is idempotent: calling it once or several times successively has the same effect (that is no side effect), where successive identical POST may have additional effects, like passing an order several times.
78+
Request has body Yes
79+
Successful response has body No
80+
Safe No
81+
Idempotent Yes
82+
Cacheable No
83+
Allowed in HTML forms No
84+
85+
https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/PUT`,
86+
},
87+
{
88+
Name: "DELETE",
89+
Summary: "The DELETE method deletes the specified resource.",
90+
Description: `The HTTP DELETE request method deletes the specified resource.
91+
Request has body May
92+
Successful response has body May
93+
Safe No
94+
Idempotent Yes
95+
Cacheable No
96+
Allowed in HTML forms No
97+
98+
https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/DELETE`,
99+
},
100+
{
101+
Name: "CONNECT",
102+
Summary: "The CONNECT method establishes a tunnel to the server identified by the target resource.",
103+
Description: `The HTTP CONNECT method starts two-way communications with the requested resource. It can be used to open a tunnel.
104+
105+
For example, the CONNECT method can be used to access websites that use SSL (HTTPS). The client asks an HTTP Proxy server to tunnel the TCP connection to the desired destination. The server then proceeds to make the connection on behalf of the client. Once the connection has been established by the server, the Proxy server continues to proxy the TCP stream to and from the client.
106+
107+
CONNECT is a hop-by-hop method.
108+
Request has body No
109+
Successful response has body Yes
110+
Safe No
111+
Idempotent No
112+
Cacheable No
113+
Allowed in HTML forms No
114+
115+
https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/CONNECT`,
116+
},
117+
{
118+
Name: "OPTIONS",
119+
Summary: "The OPTIONS method is used to describe the communication options for the target resource.",
120+
Description: `The HTTP OPTIONS method is used to describe the communication options for the target resource. The client can specify a URL for the OPTIONS method, or an asterisk (*) to refer to the entire server.
121+
Request has body No
122+
Successful response has body Yes
123+
Safe Yes
124+
Idempotent Yes
125+
Cacheable No
126+
Allowed in HTML forms No
127+
128+
https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/OPTIONS`,
129+
},
130+
{
131+
Name: "TRACE",
132+
Summary: "The TRACE method performs a message loop-back test along the path to the target resource.",
133+
Description: `The HTTP TRACE method performs a message loop-back test along the path to the target resource, providing a useful debugging mechanism.
134+
135+
The final recipient of the request should reflect the message received, excluding some fields described below, back to the client as the message body of a 200 (OK) response with a Content-Type of message/http. The final recipient is either the origin server or the first server to receive a Max-Forwards value of 0 in the request.
136+
Request has body No
137+
Successful response has body No
138+
Safe No
139+
Idempotent Yes
140+
Cacheable No
141+
Allowed in HTML forms No
142+
143+
https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/TRACE`,
144+
},
145+
{
146+
Name: "PATCH",
147+
Summary: "The PATCH method is used to apply partial modifications to a resource.",
148+
Description: `The HTTP PATCH request method applies partial modifications to a resource.
149+
150+
The HTTP PUT method only allows complete replacement of a document. Unlike PUT, PATCH is not idempotent, meaning successive identical patch requests may have different effects. However, it is possible to issue PATCH requests in such a way as to be idempotent.
151+
152+
PATCH (like PUT) may have side-effects on other resources.
153+
154+
To find out whether a server supports PATCH, a server can advertise its support by adding it to the list in the Allow or Access-Control-Allow-Methods (for CORS) response headers.
155+
156+
Another (implicit) indication that PATCH is allowed, is the presence of the Accept-Patch header, which specifies the patch document formats accepted by the server.
157+
Request has body Yes
158+
Successful response has body Yes
159+
Safe No
160+
Idempotent No
161+
Cacheable No
162+
Allowed in HTML forms No
163+
164+
https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/PATCH`,
165+
},
166+
}

test/basic.bats

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,26 @@ BIN=./httpref
1111
[ $status -eq 0 ]
1212
}
1313

14+
@test "Check that filtering from alias status works" {
15+
run ${BIN} statuses 1xx
16+
[ $status -eq 0 ]
17+
}
18+
1419
@test "Check that filtering from status works" {
1520
run ${BIN} status 1xx
1621
[ $status -eq 0 ]
1722
}
1823

24+
@test "Check that filtering from methods works" {
25+
run ${BIN} methods GET
26+
[ $status -eq 0 ]
27+
}
28+
29+
@test "Check that filtering from methods alias works" {
30+
run ${BIN} method GET
31+
[ $status -eq 0 ]
32+
}
33+
1934
@test "Check that filtering from headers works" {
2035
run ${BIN} headers Accept-Ranges
2136
[ $status -eq 0 ]

0 commit comments

Comments
 (0)