Skip to content

Commit

Permalink
Accept filepath as payload when argument starts with '@'.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ishmeet authored and GollyTicker committed May 25, 2024
1 parent 507a9a6 commit 335496f
Show file tree
Hide file tree
Showing 28 changed files with 746 additions and 362 deletions.
2 changes: 1 addition & 1 deletion EXAMPLES.md
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,7 @@ Total curl args:
=========================== POST Response Headers =========================== <<<
HTTP/1.1 200 OK
Content-Type: application/x-protobuf
Date: Tue, 20 Feb 2024 23:06:35 GMT
Date: Sat, 25 May 2024 22:15:22 GMT
Connection: keep-alive
Keep-Alive: timeout=5
Content-Length: 35
Expand Down
75 changes: 44 additions & 31 deletions TESTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,47 +113,43 @@ Running the tests might look like this:
```
$ ./test/suite/test.sh "$PWD"
Populating cache...
Established Protobuf version 24.4
Using cache...
Established go version 1.21.1
Using cache...
Established Goreleaser version v1.21.2
Using cache...
Established Latest released protoCURL version v1.7.0
Variables: VERSION=1.8.0-rc-dev, VVERSION=v1.8.0-rc-dev, SNAPSHOT=true, BUILD_ARCH=amd64, PROTO_VERSION=24.4
Established Protobuf version 27.0
Populating cache...
Established go version 1.22.3
Populating cache...
Established Goreleaser version v1.26.2
Populating cache...
Established Latest released protoCURL version v1.8.1
No protoc binaries for 27.0 found. Downloading...
Downloading https://github.com/protocolbuffers/protobuf/releases/download/v27.0/protoc-27.0-linux-aarch_64.zip ...
Extracting 27.0-linux-aarch_64 to 27.0-linux-arm64
Downloading https://github.com/protocolbuffers/protobuf/releases/download/v27.0/protoc-27.0-linux-x86_32.zip ...
Extracting 27.0-linux-x86_32 to 27.0-linux-386
Downloading https://github.com/protocolbuffers/protobuf/releases/download/v27.0/protoc-27.0-linux-x86_64.zip ...
Extracting 27.0-linux-x86_64 to 27.0-linux-amd64
Downloading https://github.com/protocolbuffers/protobuf/releases/download/v27.0/protoc-27.0-osx-aarch_64.zip ...
Extracting 27.0-osx-aarch_64 to 27.0-darwin-arm64
Downloading https://github.com/protocolbuffers/protobuf/releases/download/v27.0/protoc-27.0-osx-x86_64.zip ...
Extracting 27.0-osx-x86_64 to 27.0-darwin-amd64
Downloading https://github.com/protocolbuffers/protobuf/releases/download/v27.0/protoc-27.0-win32.zip ...
Extracting 27.0-win32 to 27.0-windows-386
Downloading https://github.com/protocolbuffers/protobuf/releases/download/v27.0/protoc-27.0-win64.zip ...
Extracting 27.0-win64 to 27.0-windows-amd64
Done.
Variables: VERSION=1.9.0-rc-dev, VVERSION=v1.9.0-rc-dev, SNAPSHOT=true, BUILD_ARCH=amd64, PROTO_VERSION=27.0
Stopping server...
Done.
Building protocurl:latest ...
sha256:cc3be835f4e8a05cc0083bdfccc2c5c09854f4dc585bdedcb780d0bf582c5c9c
sha256:f4245bec7c1599da2cb44ef212dd066d95257935d58485ebfc1cebb761a4ab52
Done.
Building test image variant of protocurl including additonal executables ...
[+] Building 0.3s (15/15) FINISHED docker:default
=> [internal] load build definition from Dockerfile 0.0s
=> => transferring dockerfile: 608B 0.0s
=> [internal] load .dockerignore 0.0s
=> => transferring context: 78B 0.0s
=> [internal] load metadata for docker.io/library/debian:11-slim 0.2s
=> [internal] load metadata for docker.io/library/protocurl:latest 0.0s
=> [builder 1/2] FROM docker.io/library/debian:11-slim@sha256:c618be84fc82aa8ba203abbb07218410b0f5b3c7cb6b4e7248fda7785d4f9946 0.0s
=> [final 1/8] FROM docker.io/library/protocurl:latest 0.0s
=> CACHED [builder 2/2] RUN apt-get update && apt-get install -y inotify-tools procps 0.0s
=> CACHED [final 2/8] COPY --from=builder /bin/* /bin/ 0.0s
=> CACHED [final 3/8] COPY --from=builder /usr/bin/* /usr/bin/ 0.0s
=> CACHED [final 4/8] COPY --from=builder /lib/*-linux-gnu /lib/x86_64-linux-gnu/ 0.0s
=> CACHED [final 5/8] COPY --from=builder /lib/*-linux-gnu /lib/aarch_64-linux-gnu/ 0.0s
=> CACHED [final 6/8] COPY --from=builder /usr/lib/*-linux-gnu /usr/lib/x86_64-linux-gnu/ 0.0s
=> CACHED [final 7/8] COPY --from=builder /usr/lib/*-linux-gnu /usr/lib/aarch_64-linux-gnu/ 0.0s
=> CACHED [final 8/8] COPY --from=builder /lib64*/ld-linux-*.so.2 /lib64/ 0.0s
=> exporting to image 0.0s
=> => exporting layers 0.0s
=> => writing image sha256:1c56da99fb8a4e5cc3303bb9b4170254e6e962758c96fcf7718a6dfc08a3c999 0.0s
=> => naming to docker.io/library/protocurl:latest-test 0.0s
sha256:d69bfe0bfd502082e3eb249eb1ee26f5ced21b4ec2ad71d74f8b748e5f000b1d
Done.
Starting server...
Done.
Waiting for server to become ready...
Waited 1 seconds already...
Waited 2 seconds already...
Waited 3 seconds already...
=== Test server is ready ===
=== Running ALL Tests ===
✨✨✨ SUCCESS ✨✨✨ - wednesday-is-not-a-happy-day
Expand Down Expand Up @@ -204,6 +200,12 @@ Waited 3 seconds already...
✨✨✨ SUCCESS ✨✨✨ - wednesday-is-not-a-happy-day-json
✨✨✨ SUCCESS ✨✨✨ - wednesday-is-not-a-happy-day-json-no-curl
✨✨✨ SUCCESS ✨✨✨ - wednesday-is-not-a-happy-day-json--X_GET
✨✨✨ SUCCESS ✨✨✨ - payload-json
✨✨✨ SUCCESS ✨✨✨ - payload-json--v
✨✨✨ SUCCESS ✨✨✨ - payload-json-relative
✨✨✨ SUCCESS ✨✨✨ - payload-txt
✨✨✨ SUCCESS ✨✨✨ - payload-invalid
✨✨✨ SUCCESS ✨✨✨ - payload-file-not-found
✨✨✨ SUCCESS ✨✨✨ - in-wrong
✨✨✨ SUCCESS ✨✨✨ - in-wrong--X_GET
✨✨✨ SUCCESS ✨✨✨ - out-wrong
Expand Down Expand Up @@ -250,6 +252,9 @@ Waited 3 seconds already...
✨✨✨ SUCCESS ✨✨✨ - quiet-with-content
✨✨✨ SUCCESS ✨✨✨ - quiet-with-content-no-curl
✨✨✨ SUCCESS ✨✨✨ - quiet-with-content--X_GET
✨✨✨ SUCCESS ✨✨✨ - silent-with-content
✨✨✨ SUCCESS ✨✨✨ - silent-with-content-no-curl
✨✨✨ SUCCESS ✨✨✨ - silent-with-content--X_GET
✨✨✨ SUCCESS ✨✨✨ - display-binary-and-headers
✨✨✨ SUCCESS ✨✨✨ - display-binary-and-headers-no-curl
✨✨✨ SUCCESS ✨✨✨ - display-binary-and-headers--X_GET
Expand Down Expand Up @@ -313,8 +318,16 @@ Waited 3 seconds already...
✨✨✨ SUCCESS ✨✨✨ - failure-simple-no-curl
✨✨✨ SUCCESS ✨✨✨ - failure-simple--X_GET
✨✨✨ SUCCESS ✨✨✨ - failure-simple-quiet
✨✨✨ SUCCESS ✨✨✨ - failure-simple-quiet--D
✨✨✨ SUCCESS ✨✨✨ - failure-simple-quiet--v
✨✨✨ SUCCESS ✨✨✨ - failure-simple-quiet-no-curl
✨✨✨ SUCCESS ✨✨✨ - failure-simple-quiet--X_GET
✨✨✨ SUCCESS ✨✨✨ - failure-simple-silent
✨✨✨ SUCCESS ✨✨✨ - failure-simple-silent--D
✨✨✨ SUCCESS ✨✨✨ - failure-simple-silent--v
✨✨✨ SUCCESS ✨✨✨ - failure-simple-silent--q
✨✨✨ SUCCESS ✨✨✨ - failure-simple-silent-no-curl
✨✨✨ SUCCESS ✨✨✨ - failure-simple-silent--X_GET
✨✨✨ SUCCESS ✨✨✨ - missing-args
✨✨✨ SUCCESS ✨✨✨ - missing-args-no-curl
✨✨✨ SUCCESS ✨✨✨ - missing-args--X_GET
Expand Down
50 changes: 25 additions & 25 deletions doc/generated.usage.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,28 +19,28 @@ Examples:
protocurl -I my-protos -i package.path.Req -o package.path.Resp -u http://example.com/api -d "myField: true, otherField: 1337"

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 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 and GET are explicitly supported. Other methods are passed 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.
-I, --proto-dir string Uses the specified directory to find the proto-file. (default "/proto")
-f, --proto-file string Uses the specified file path to find the Protobuf definition of the message types within 'proto-dir' (relative file path).
--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 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.
-s, --silent Suppresses all output on stdout. Overrides and deactivates -v, -D and -q. Errors are still printed to stderr.
-u, --url string Mandatory: The url to send the request to
-v, --verbose Prints version and enables verbose output. Also activates -D.
--version version for protocurl
--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-or-file string The payload data in Protobuf text format or JSON supplied as a string or a filepath (if first character is '@'). The string 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 and GET are explicitly supported. Other methods are passed 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.
-I, --proto-dir string Uses the specified directory to find the proto-file. (default "/proto")
-f, --proto-file string Uses the specified file path to find the Protobuf definition of the message types within 'proto-dir' (relative file path).
--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 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.
-s, --silent Suppresses all output on stdout. Overrides and deactivates -v, -D and -q. Errors are still printed to stderr.
-u, --url string Mandatory: The url to send the request to
-v, --verbose Prints version and enables verbose output. Also activates -D.
--version version for protocurl
2 changes: 2 additions & 0 deletions doc/template.README.md
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,8 @@ How to make a release: [RELEASE.md](RELEASE.md)

- As for script utilities, one needs `bash`, `jq`, `zip`, `unzip` and `curl`.

Please make sure, that docker including the docker compose plugin is installed. The setup works with docker `v26.1.3` and docker compose `v2.27.0`,

Running the tests will download the protoc binaries on the first invocation automatically.

For development the `generated-local.Dockerfile` (via [generate-local.Dockerfile.sh](dev/generate-local.Dockerfile.sh)) is used.
Expand Down
17 changes: 14 additions & 3 deletions src/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"fmt"
"os"
"strings"
)

Expand Down Expand Up @@ -85,9 +86,9 @@ func intialiseFlags() {
"Mandatory: The url to send the request to")
AssertSuccess(rootCmd.MarkFlagRequired("url"))

flags.StringVarP(&CurrentConfig.DataText, "data-text", "d", "",
"The payload data in Protobuf text format or JSON. "+
"It is inferred from the input as JSON if the first token is a '{'. "+
flags.StringVarP(&CurrentConfig.DataText, "data-text-or-file", "d", "",
"The payload data in Protobuf text format or JSON supplied as a string or a filepath (if first character is '@'). "+
"The string 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 "+GithubRepositoryLink)

Expand Down Expand Up @@ -170,6 +171,16 @@ func propagateFlags() {
}
}

if strings.HasPrefix(CurrentConfig.DataText, "@") {
filepath := CurrentConfig.DataText[1:]
if CurrentConfig.Verbose {
fmt.Printf("Input text will be read from file %s.\n", filepath)
}
file, err := os.ReadFile(filepath)
PanicOnError(err)
CurrentConfig.DataText = string(file) // assumes UTF-8
}

if CurrentConfig.DataText != "" && CurrentConfig.RequestType == "" {
PanicWithMessage("Non-empty data-body was provided, but no request type was given. Hence, encoding of data-body is not possible.")
}
Expand Down
1 change: 1 addition & 0 deletions test/payloads/invalid.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
includeRe
4 changes: 4 additions & 0 deletions test/payloads/payload.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"includeReason":true,
"date": "2022-03-23T14:15:39Z"
}
1 change: 1 addition & 0 deletions test/payloads/payload.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
includeReason: true, date: { seconds: 1648044939, nanos: 152000000 }
Loading

0 comments on commit 335496f

Please sign in to comment.