Skip to content

Commit

Permalink
Make body and request type non-mandatory to support GET requests.
Browse files Browse the repository at this point in the history
  • Loading branch information
GollyTicker committed Oct 4, 2023
1 parent 60b0a87 commit 93f878a
Show file tree
Hide file tree
Showing 38 changed files with 126 additions and 589 deletions.
6 changes: 3 additions & 3 deletions EXAMPLES.md
Expand Up @@ -423,19 +423,19 @@ Total curl args:
-s
-X
POST
--data-binary
@<tmp>
--output
<tmp>
--dump-header
<tmp>
--data-binary
@<tmp>
-H
Content-Type: application/x-protobuf
http://localhost:8080/happy-day/verify
=========================== POST Response Headers =========================== <<<
HTTP/1.1 200 OK
Content-Type: application/x-protobuf
Date: Tue, 03 Oct 2023 09:29:50 GMT
Date: Tue, 03 Oct 2023 20:46:17 GMT
Connection: keep-alive
Keep-Alive: timeout=5
Content-Length: 35
Expand Down
6 changes: 3 additions & 3 deletions doc/generated.usage.txt
Expand Up @@ -22,13 +22,13 @@ Flags:
--curl Forces the use of curl executable found in PATH. If none was found, then exits with an error.
-C, --curl-args string Additional cURL args which will be passed on to cURL during request invocation for further configuration. Also activates --curl.
--curl-path string Uses the given path to invoke curl instead of searching for curl in PATH. Also activates --curl.
-d, --data-text string Mandatory: The payload data in Protobuf text format or JSON. It is inferred from the input as JSON if the first token is a '{'. The format can be set explicitly via --in. See https://github.com/qaware/protocurl
-d, --data-text string The payload data in Protobuf text format or JSON. It is inferred from the input as JSON if the first token is a '{'. The format can be set explicitly via --in. Mandatory if request-type is provided.See https://github.com/qaware/protocurl
--decode-raw Decode the response into textual format without the schema by only showing field numbers and inferred field types. Types may be incorrect. Only output format text is supported. Use -o <response-type> to see correct contents.
-D, --display-binary-and-http Displays the binary request and response as well as the non-binary response headers.
-h, --help help for protocurl
--in string Specifies, in which format the input -d should be interpreted in. 'text' (default) uses the Protobuf text format and 'json' uses JSON. The type is inferred as JSON if the first token is a '{'.
-F, --infer-files Infer the correct files containing the relevant protobuf messages. All proto files in the proto directory provided by -I will be used. If no -f <file> is provided, this -F is set and the files are inferred.
-X, --method string HTTP request method. POST (default) and GET supported. Other methods are passed on on to curl optimistically. (default "POST")
-X, --method string HTTP request method. POST and GET are explicitly supported. Other methods are passed on on to curl optimistically. (default "POST")
--no-curl Forces the use of the built-in internal http request instead of curl.
-n, --no-default-headers Default headers (e.g. "Content-Type") will not be passed to curl. Assumes --curl. Use "-n -H 'Content-Type: FooBar'" to override the default content type.
--out string Produces the output in the specified format. 'text' (default) produces Protobuf text format. 'json' produces dense JSON and 'json:pretty' produces pretty-printed JSON. The produced JSON always uses the original Protobuf field names instead of lowerCamelCasing them.
Expand All @@ -37,7 +37,7 @@ Flags:
--protoc Forces the use of a global protoc executable found in PATH or via --protoc-path instead of using the bundled one. If none was found, then exits with an error.
--protoc-path string Uses the given path to invoke protoc instead of searching for protoc in PATH. Also activates --protoc.
-H, --request-header string Adds the string header to the invocation of cURL. This option is not supported when --no-curl is active. E.g. -H 'MyHeader: FooBar'.
-i, --request-type string Mandatory: Message name or full package path of the Protobuf request type. The path can be shortened to '..', if the name of the request message is unique. E.g. mypackage.MyRequest or ..MyRequest
-i, --request-type string Message name or full package path of the Protobuf request type. The path can be shortened to '..', if the name of the request message is unique. Mandatory for POST requests. E.g. mypackage.MyRequest or ..MyRequest
-o, --response-type string The Protobuf response type. See -i <request-type>. Overrides --decode-raw. If not set, then --decode-raw is used.
-q, --show-output-only Suppresses all output except response Protobuf as text. Overrides and deactivates -v and -D. Errors are still printed to stderr.
-u, --url string Mandatory: The url to send the request to
Expand Down
21 changes: 15 additions & 6 deletions src/flags.go
Expand Up @@ -61,11 +61,10 @@ func intialiseFlags() {
"Infer the correct files containing the relevant protobuf messages. All proto files in the proto directory provided by -I will be used. If no -f <file> is provided, this -F is set and the files are inferred.")

flags.StringVarP(&CurrentConfig.Method, "method", "X", "POST",
"HTTP request method. POST (default) and GET supported. Other methods are passed on on to curl optimistically.")
"HTTP request method. POST and GET are explicitly supported. Other methods are passed on on to curl optimistically.")

flags.StringVarP(&CurrentConfig.RequestType, "request-type", "i", "",
"Mandatory: Message name or full package path of the Protobuf request type. The path can be shortened to '..', if the name of the request message is unique. E.g. mypackage.MyRequest or ..MyRequest")
AssertSuccess(rootCmd.MarkFlagRequired("request-type"))
"Message name or full package path of the Protobuf request type. The path can be shortened to '..', if the name of the request message is unique. Mandatory for POST requests. E.g. mypackage.MyRequest or ..MyRequest")

flags.StringVarP(&CurrentConfig.ResponseType, "response-type", "o", "",
"The Protobuf response type. See -i <request-type>. Overrides --decode-raw. If not set, then --decode-raw is used.")
Expand All @@ -87,10 +86,10 @@ func intialiseFlags() {
AssertSuccess(rootCmd.MarkFlagRequired("url"))

flags.StringVarP(&CurrentConfig.DataText, "data-text", "d", "",
"Mandatory: The payload data in Protobuf text format or JSON. "+
"The payload data in Protobuf text format or JSON. "+
"It is inferred from the input as JSON if the first token is a '{'. "+
"The format can be set explicitly via --in. See "+GithubRepositoryLink)
AssertSuccess(rootCmd.MarkFlagRequired("data-text"))
"The format can be set explicitly via --in. Mandatory if request-type is provided."+
"See "+GithubRepositoryLink)

flags.BoolVarP(&CurrentConfig.NoDefaultHeaders, "no-default-headers", "n", false,
"Default headers (e.g. \"Content-Type\") will not be passed to curl. Assumes --curl. Use \"-n -H 'Content-Type: FooBar'\" to override the default content type.")
Expand Down Expand Up @@ -155,6 +154,16 @@ func propagateFlags() {
fmt.Printf("Got method %s which is not explicitly supported. Proceeding optimistically.", CurrentConfig.Method)
}

if CurrentConfig.Method == "POST" {
if CurrentConfig.RequestType == "" {
PanicWithMessage("With method POST, a request type and the data text is needed. However, request type was not provided. Aborting.")
}
}

if CurrentConfig.DataText != "" && CurrentConfig.RequestType == "" {
PanicWithMessage("Non-empty data-body as provided, but no request type was given. Hence, encoding of data-body is not possible.")
}

if strings.HasPrefix(strings.TrimSpace(CurrentConfig.DataText), "{") {
tmpDataTextInferredType = IJson
} else {
Expand Down
21 changes: 19 additions & 2 deletions src/httpRequest.go
Expand Up @@ -28,7 +28,20 @@ func invokeInternalHttpRequest(requestBinary []byte) ([]byte, string) {
PanicDueToUnsupportedHeadersWhenInternalHttp(CurrentConfig.RequestHeaders)
}

httpResponse, err := http.Post(CurrentConfig.Url, DefaultContentType, bytes.NewReader(requestBinary))
var httpResponse *http.Response
var err error
switch CurrentConfig.Method {
case "GET":
if CurrentConfig.RequestType != "" {
PanicWithMessage("Internal Http implementation doesn't support GET requests with body. Please use curl.")
}
httpResponse, err = http.Get(CurrentConfig.Url)
case "POST":
httpResponse, err = http.Post(CurrentConfig.Url, DefaultContentType, bytes.NewReader(requestBinary))
default:
PanicWithMessage("HTTP method " + CurrentConfig.Method + " not supported with internal HTTP implementation. Please use curl.")
}

PanicWithMessageOnError(err, func() string { return "Failed internal HTTP request. Error: " + err.Error() })
defer func() { _ = httpResponse.Body.Close() }()

Expand Down Expand Up @@ -68,10 +81,14 @@ func invokeCurlRequest(requestBinary []byte, curlPath string) ([]byte, string) {
curlPath,
"-s",
"-X", CurrentConfig.Method,
"--data-binary", "@" + requestBinaryFile,
"--output", responseBinaryFile,
"--dump-header", responseHeadersTextFile,
}

if CurrentConfig.RequestType != "" {
curlArgs = append(curlArgs, "--data-binary", "@"+requestBinaryFile)
}

for _, header := range CurrentConfig.RequestHeaders {
curlArgs = append(curlArgs, "-H", header)
}
Expand Down
7 changes: 6 additions & 1 deletion src/protocurl.go
Expand Up @@ -90,7 +90,12 @@ var rootCmd = &cobra.Command{
func runProtocurlWorkflow() {
protoRegistryFiles := convertProtoFilesToProtoRegistryFiles()

requestBinary := encodeToBinary(CurrentConfig.RequestType, CurrentConfig.DataText, protoRegistryFiles)
var requestBinary []byte // empty iff no body was provided or if an empty body was provided.
if CurrentConfig.Method == "GET" && CurrentConfig.RequestType == "" {
requestBinary = []byte{}
} else {
requestBinary = encodeToBinary(CurrentConfig.RequestType, CurrentConfig.DataText, protoRegistryFiles)
}

responseBinary, responseHeaders := invokeHttpRequestBasedOnConfig(requestBinary)

Expand Down
2 changes: 1 addition & 1 deletion test/results/additional-curl-args-expected.txt
Expand Up @@ -10,7 +10,7 @@ includeReason: true
=========================== POST Response Headers =========================== <<<
HTTP/1.1 200 OK
Content-Type: application/x-protobuf
Date: Fri, 29 Sep 2023 23:20:05 GMT
Date: Tue, 03 Oct 2023 20:42:35 GMT
Connection: keep-alive
Keep-Alive: timeout=5
Content-Length: 65
Expand Down
8 changes: 4 additions & 4 deletions test/results/additional-curl-args-verbose-expected.txt
Expand Up @@ -251,12 +251,12 @@ Total curl args:
-s
-X
POST
--data-binary
@<tmp>
--output
<tmp>
--dump-header
<tmp>
--data-binary
@<tmp>
-H
Content-Type: application/x-protobuf
-v
Expand All @@ -279,7 +279,7 @@ Total curl args:
* Mark bundle as not supporting multiuse
< HTTP/1.1 200 OK
< Content-Type: application/x-protobuf
< Date: Fri, 29 Sep 2023 23:20:05 GMT
< Date: Tue, 03 Oct 2023 20:42:36 GMT
< Connection: keep-alive
< Keep-Alive: timeout=5
< Content-Length: 65
Expand All @@ -290,7 +290,7 @@ Total curl args:
=========================== POST Response Headers =========================== <<<
HTTP/1.1 200 OK
Content-Type: application/x-protobuf
Date: Fri, 29 Sep 2023 23:20:05 GMT
Date: Tue, 03 Oct 2023 20:42:36 GMT
Connection: keep-alive
Keep-Alive: timeout=5
Content-Length: 65
Expand Down
2 changes: 1 addition & 1 deletion test/results/display-binary-and-headers-expected.txt
Expand Up @@ -10,7 +10,7 @@ includeReason: true
=========================== POST Response Headers =========================== <<<
HTTP/1.1 200 OK
Content-Type: application/x-protobuf
Date: Fri, 29 Sep 2023 23:20:04 GMT
Date: Tue, 03 Oct 2023 20:42:35 GMT
Connection: keep-alive
Keep-Alive: timeout=5
Content-Length: 65
Expand Down
Expand Up @@ -12,7 +12,7 @@ HTTP/1.1 200 OK
Content-Length: 65
Connection: keep-alive
Content-Type: application/x-protobuf
Date: Fri, 29 Sep 2023 23:20:04 GMT
Date: Tue, 03 Oct 2023 20:42:35 GMT
Keep-Alive: timeout=5
=========================== POST Response Binary =========================== <<<
00000000 08 01 12 1c 54 68 75 72 73 64 61 79 20 69 73 20 |....Thursday is |
Expand Down
2 changes: 1 addition & 1 deletion test/results/echo-empty-with-curl-args-expected.txt
Expand Up @@ -16,7 +16,7 @@ includeReason: true
* Mark bundle as not supporting multiuse
< HTTP/1.1 200 OK
< Content-Type: application/x-protobuf
< Date: Fri, 29 Sep 2023 23:20:13 GMT
< Date: Tue, 03 Oct 2023 20:42:44 GMT
< Connection: keep-alive
< Keep-Alive: timeout=5
< Content-Length: 2
Expand Down
6 changes: 3 additions & 3 deletions test/results/far-future-json--v-expected.txt
Expand Up @@ -246,19 +246,19 @@ Total curl args:
-s
-X
POST
--data-binary
@<tmp>
--output
<tmp>
--dump-header
<tmp>
--data-binary
@<tmp>
-H
Content-Type: application/x-protobuf
http://localhost:8080/happy-day/verify
=========================== POST Response Headers =========================== <<<
HTTP/1.1 200 OK
Content-Type: application/x-protobuf
Date: Fri, 29 Sep 2023 23:20:07 GMT
Date: Tue, 03 Oct 2023 20:42:38 GMT
Connection: keep-alive
Keep-Alive: timeout=5
Content-Length: 63
Expand Down
5 changes: 0 additions & 5 deletions test/results/far-future-json-no-curl-expected.txt

This file was deleted.

6 changes: 3 additions & 3 deletions test/results/help-expected.txt
Expand Up @@ -23,13 +23,13 @@ Flags:
--curl Forces the use of curl executable found in PATH. If none was found, then exits with an error.
-C, --curl-args string Additional cURL args which will be passed on to cURL during request invocation for further configuration. Also activates --curl.
--curl-path string Uses the given path to invoke curl instead of searching for curl in PATH. Also activates --curl.
-d, --data-text string Mandatory: The payload data in Protobuf text format or JSON. It is inferred from the input as JSON if the first token is a '{'. The format can be set explicitly via --in. See https://github.com/qaware/protocurl
-d, --data-text string The payload data in Protobuf text format or JSON. It is inferred from the input as JSON if the first token is a '{'. The format can be set explicitly via --in. Mandatory if request-type is provided.See https://github.com/qaware/protocurl
--decode-raw Decode the response into textual format without the schema by only showing field numbers and inferred field types. Types may be incorrect. Only output format text is supported. Use -o <response-type> to see correct contents.
-D, --display-binary-and-http Displays the binary request and response as well as the non-binary response headers.
-h, --help help for protocurl
--in string Specifies, in which format the input -d should be interpreted in. 'text' (default) uses the Protobuf text format and 'json' uses JSON. The type is inferred as JSON if the first token is a '{'.
-F, --infer-files Infer the correct files containing the relevant protobuf messages. All proto files in the proto directory provided by -I will be used. If no -f <file> is provided, this -F is set and the files are inferred.
-X, --method string HTTP request method. POST (default) and GET supported. Other methods are passed on on to curl optimistically. (default "POST")
-X, --method string HTTP request method. POST and GET are explicitly supported. Other methods are passed on on to curl optimistically. (default "POST")
--no-curl Forces the use of the built-in internal http request instead of curl.
-n, --no-default-headers Default headers (e.g. "Content-Type") will not be passed to curl. Assumes --curl. Use "-n -H 'Content-Type: FooBar'" to override the default content type.
--out string Produces the output in the specified format. 'text' (default) produces Protobuf text format. 'json' produces dense JSON and 'json:pretty' produces pretty-printed JSON. The produced JSON always uses the original Protobuf field names instead of lowerCamelCasing them.
Expand All @@ -38,7 +38,7 @@ Flags:
--protoc Forces the use of a global protoc executable found in PATH or via --protoc-path instead of using the bundled one. If none was found, then exits with an error.
--protoc-path string Uses the given path to invoke protoc instead of searching for protoc in PATH. Also activates --protoc.
-H, --request-header string Adds the string header to the invocation of cURL. This option is not supported when --no-curl is active. E.g. -H 'MyHeader: FooBar'.
-i, --request-type string Mandatory: Message name or full package path of the Protobuf request type. The path can be shortened to '..', if the name of the request message is unique. E.g. mypackage.MyRequest or ..MyRequest
-i, --request-type string Message name or full package path of the Protobuf request type. The path can be shortened to '..', if the name of the request message is unique. Mandatory for POST requests. E.g. mypackage.MyRequest or ..MyRequest
-o, --response-type string The Protobuf response type. See -i <request-type>. Overrides --decode-raw. If not set, then --decode-raw is used.
-q, --show-output-only Suppresses all output except response Protobuf as text. Overrides and deactivates -v and -D. Errors are still printed to stderr.
-u, --url string Mandatory: The url to send the request to
Expand Down

0 comments on commit 93f878a

Please sign in to comment.