Skip to content

Commit 9fd2c69

Browse files
authored
File as payload (#182)
Closes #173
2 parents 507a9a6 + a4ffc81 commit 9fd2c69

31 files changed

+751
-367
lines changed

EXAMPLES.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -436,7 +436,7 @@ Total curl args:
436436
=========================== POST Response Headers =========================== <<<
437437
HTTP/1.1 200 OK
438438
Content-Type: application/x-protobuf
439-
Date: Tue, 20 Feb 2024 23:06:35 GMT
439+
Date: Sat, 25 May 2024 22:15:22 GMT
440440
Connection: keep-alive
441441
Keep-Alive: timeout=5
442442
Content-Length: 35

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,8 @@ How to make a release: [RELEASE.md](RELEASE.md)
208208

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

211+
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`,
212+
211213
Running the tests will download the protoc binaries on the first invocation automatically.
212214

213215
For development the `generated-local.Dockerfile` (via [generate-local.Dockerfile.sh](dev/generate-local.Dockerfile.sh)) is used.

TESTS.md

Lines changed: 44 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -113,47 +113,43 @@ Running the tests might look like this:
113113
```
114114
$ ./test/suite/test.sh "$PWD"
115115
Populating cache...
116-
Established Protobuf version 24.4
117-
Using cache...
118-
Established go version 1.21.1
119-
Using cache...
120-
Established Goreleaser version v1.21.2
121-
Using cache...
122-
Established Latest released protoCURL version v1.7.0
123-
Variables: VERSION=1.8.0-rc-dev, VVERSION=v1.8.0-rc-dev, SNAPSHOT=true, BUILD_ARCH=amd64, PROTO_VERSION=24.4
116+
Established Protobuf version 27.0
117+
Populating cache...
118+
Established go version 1.22.3
119+
Populating cache...
120+
Established Goreleaser version v1.26.2
121+
Populating cache...
122+
Established Latest released protoCURL version v1.8.1
123+
No protoc binaries for 27.0 found. Downloading...
124+
Downloading https://github.com/protocolbuffers/protobuf/releases/download/v27.0/protoc-27.0-linux-aarch_64.zip ...
125+
Extracting 27.0-linux-aarch_64 to 27.0-linux-arm64
126+
Downloading https://github.com/protocolbuffers/protobuf/releases/download/v27.0/protoc-27.0-linux-x86_32.zip ...
127+
Extracting 27.0-linux-x86_32 to 27.0-linux-386
128+
Downloading https://github.com/protocolbuffers/protobuf/releases/download/v27.0/protoc-27.0-linux-x86_64.zip ...
129+
Extracting 27.0-linux-x86_64 to 27.0-linux-amd64
130+
Downloading https://github.com/protocolbuffers/protobuf/releases/download/v27.0/protoc-27.0-osx-aarch_64.zip ...
131+
Extracting 27.0-osx-aarch_64 to 27.0-darwin-arm64
132+
Downloading https://github.com/protocolbuffers/protobuf/releases/download/v27.0/protoc-27.0-osx-x86_64.zip ...
133+
Extracting 27.0-osx-x86_64 to 27.0-darwin-amd64
134+
Downloading https://github.com/protocolbuffers/protobuf/releases/download/v27.0/protoc-27.0-win32.zip ...
135+
Extracting 27.0-win32 to 27.0-windows-386
136+
Downloading https://github.com/protocolbuffers/protobuf/releases/download/v27.0/protoc-27.0-win64.zip ...
137+
Extracting 27.0-win64 to 27.0-windows-amd64
138+
Done.
139+
Variables: VERSION=1.9.0-rc-dev, VVERSION=v1.9.0-rc-dev, SNAPSHOT=true, BUILD_ARCH=amd64, PROTO_VERSION=27.0
124140
Stopping server...
125141
Done.
126142
Building protocurl:latest ...
127-
sha256:cc3be835f4e8a05cc0083bdfccc2c5c09854f4dc585bdedcb780d0bf582c5c9c
143+
sha256:f4245bec7c1599da2cb44ef212dd066d95257935d58485ebfc1cebb761a4ab52
128144
Done.
129145
Building test image variant of protocurl including additonal executables ...
130-
[+] Building 0.3s (15/15) FINISHED docker:default
131-
=> [internal] load build definition from Dockerfile 0.0s
132-
=> => transferring dockerfile: 608B 0.0s
133-
=> [internal] load .dockerignore 0.0s
134-
=> => transferring context: 78B 0.0s
135-
=> [internal] load metadata for docker.io/library/debian:11-slim 0.2s
136-
=> [internal] load metadata for docker.io/library/protocurl:latest 0.0s
137-
=> [builder 1/2] FROM docker.io/library/debian:11-slim@sha256:c618be84fc82aa8ba203abbb07218410b0f5b3c7cb6b4e7248fda7785d4f9946 0.0s
138-
=> [final 1/8] FROM docker.io/library/protocurl:latest 0.0s
139-
=> CACHED [builder 2/2] RUN apt-get update && apt-get install -y inotify-tools procps 0.0s
140-
=> CACHED [final 2/8] COPY --from=builder /bin/* /bin/ 0.0s
141-
=> CACHED [final 3/8] COPY --from=builder /usr/bin/* /usr/bin/ 0.0s
142-
=> CACHED [final 4/8] COPY --from=builder /lib/*-linux-gnu /lib/x86_64-linux-gnu/ 0.0s
143-
=> CACHED [final 5/8] COPY --from=builder /lib/*-linux-gnu /lib/aarch_64-linux-gnu/ 0.0s
144-
=> CACHED [final 6/8] COPY --from=builder /usr/lib/*-linux-gnu /usr/lib/x86_64-linux-gnu/ 0.0s
145-
=> CACHED [final 7/8] COPY --from=builder /usr/lib/*-linux-gnu /usr/lib/aarch_64-linux-gnu/ 0.0s
146-
=> CACHED [final 8/8] COPY --from=builder /lib64*/ld-linux-*.so.2 /lib64/ 0.0s
147-
=> exporting to image 0.0s
148-
=> => exporting layers 0.0s
149-
=> => writing image sha256:1c56da99fb8a4e5cc3303bb9b4170254e6e962758c96fcf7718a6dfc08a3c999 0.0s
150-
=> => naming to docker.io/library/protocurl:latest-test 0.0s
146+
sha256:d69bfe0bfd502082e3eb249eb1ee26f5ced21b4ec2ad71d74f8b748e5f000b1d
151147
Done.
152148
Starting server...
153149
Done.
154150
Waiting for server to become ready...
151+
Waited 1 seconds already...
155152
Waited 2 seconds already...
156-
Waited 3 seconds already...
157153
=== Test server is ready ===
158154
=== Running ALL Tests ===
159155
✨✨✨ SUCCESS ✨✨✨ - wednesday-is-not-a-happy-day
@@ -204,6 +200,12 @@ Waited 3 seconds already...
204200
✨✨✨ SUCCESS ✨✨✨ - wednesday-is-not-a-happy-day-json
205201
✨✨✨ SUCCESS ✨✨✨ - wednesday-is-not-a-happy-day-json-no-curl
206202
✨✨✨ SUCCESS ✨✨✨ - wednesday-is-not-a-happy-day-json--X_GET
203+
✨✨✨ SUCCESS ✨✨✨ - payload-json
204+
✨✨✨ SUCCESS ✨✨✨ - payload-json--v
205+
✨✨✨ SUCCESS ✨✨✨ - payload-json-relative
206+
✨✨✨ SUCCESS ✨✨✨ - payload-txt
207+
✨✨✨ SUCCESS ✨✨✨ - payload-invalid
208+
✨✨✨ SUCCESS ✨✨✨ - payload-file-not-found
207209
✨✨✨ SUCCESS ✨✨✨ - in-wrong
208210
✨✨✨ SUCCESS ✨✨✨ - in-wrong--X_GET
209211
✨✨✨ SUCCESS ✨✨✨ - out-wrong
@@ -250,6 +252,9 @@ Waited 3 seconds already...
250252
✨✨✨ SUCCESS ✨✨✨ - quiet-with-content
251253
✨✨✨ SUCCESS ✨✨✨ - quiet-with-content-no-curl
252254
✨✨✨ SUCCESS ✨✨✨ - quiet-with-content--X_GET
255+
✨✨✨ SUCCESS ✨✨✨ - silent-with-content
256+
✨✨✨ SUCCESS ✨✨✨ - silent-with-content-no-curl
257+
✨✨✨ SUCCESS ✨✨✨ - silent-with-content--X_GET
253258
✨✨✨ SUCCESS ✨✨✨ - display-binary-and-headers
254259
✨✨✨ SUCCESS ✨✨✨ - display-binary-and-headers-no-curl
255260
✨✨✨ SUCCESS ✨✨✨ - display-binary-and-headers--X_GET
@@ -313,8 +318,16 @@ Waited 3 seconds already...
313318
✨✨✨ SUCCESS ✨✨✨ - failure-simple-no-curl
314319
✨✨✨ SUCCESS ✨✨✨ - failure-simple--X_GET
315320
✨✨✨ SUCCESS ✨✨✨ - failure-simple-quiet
321+
✨✨✨ SUCCESS ✨✨✨ - failure-simple-quiet--D
322+
✨✨✨ SUCCESS ✨✨✨ - failure-simple-quiet--v
316323
✨✨✨ SUCCESS ✨✨✨ - failure-simple-quiet-no-curl
317324
✨✨✨ SUCCESS ✨✨✨ - failure-simple-quiet--X_GET
325+
✨✨✨ SUCCESS ✨✨✨ - failure-simple-silent
326+
✨✨✨ SUCCESS ✨✨✨ - failure-simple-silent--D
327+
✨✨✨ SUCCESS ✨✨✨ - failure-simple-silent--v
328+
✨✨✨ SUCCESS ✨✨✨ - failure-simple-silent--q
329+
✨✨✨ SUCCESS ✨✨✨ - failure-simple-silent-no-curl
330+
✨✨✨ SUCCESS ✨✨✨ - failure-simple-silent--X_GET
318331
✨✨✨ SUCCESS ✨✨✨ - missing-args
319332
✨✨✨ SUCCESS ✨✨✨ - missing-args-no-curl
320333
✨✨✨ SUCCESS ✨✨✨ - missing-args--X_GET

doc/generated.usage.txt

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -19,28 +19,28 @@ Examples:
1919
protocurl -I my-protos -i package.path.Req -o package.path.Resp -u http://example.com/api -d "myField: true, otherField: 1337"
2020

2121
Flags:
22-
--curl Forces the use of curl executable found in PATH. If none was found, then exits with an error.
23-
-C, --curl-args string Additional cURL args which will be passed on to cURL during request invocation for further configuration. Also activates --curl.
24-
--curl-path string Uses the given path to invoke curl instead of searching for curl in PATH. Also activates --curl.
25-
-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
26-
--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.
27-
-D, --display-binary-and-http Displays the binary request and response as well as the non-binary response headers.
28-
-h, --help help for protocurl
29-
--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 '{'.
30-
-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.
31-
-X, --method string HTTP request method. POST and GET are explicitly supported. Other methods are passed on to curl optimistically. (default "POST")
32-
--no-curl Forces the use of the built-in internal http request instead of curl.
33-
-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.
34-
--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.
35-
-I, --proto-dir string Uses the specified directory to find the proto-file. (default "/proto")
36-
-f, --proto-file string Uses the specified file path to find the Protobuf definition of the message types within 'proto-dir' (relative file path).
37-
--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.
38-
--protoc-path string Uses the given path to invoke protoc instead of searching for protoc in PATH. Also activates --protoc.
39-
-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'.
40-
-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
41-
-o, --response-type string The Protobuf response type. See -i <request-type>. Overrides --decode-raw. If not set, then --decode-raw is used.
42-
-q, --show-output-only Suppresses all output except response Protobuf as text. Overrides and deactivates -v and -D. Errors are still printed to stderr.
43-
-s, --silent Suppresses all output on stdout. Overrides and deactivates -v, -D and -q. Errors are still printed to stderr.
44-
-u, --url string Mandatory: The url to send the request to
45-
-v, --verbose Prints version and enables verbose output. Also activates -D.
46-
--version version for protocurl
22+
--curl Forces the use of curl executable found in PATH. If none was found, then exits with an error.
23+
-C, --curl-args string Additional cURL args which will be passed on to cURL during request invocation for further configuration. Also activates --curl.
24+
--curl-path string Uses the given path to invoke curl instead of searching for curl in PATH. Also activates --curl.
25+
-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
26+
--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.
27+
-D, --display-binary-and-http Displays the binary request and response as well as the non-binary response headers.
28+
-h, --help help for protocurl
29+
--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 '{'.
30+
-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.
31+
-X, --method string HTTP request method. POST and GET are explicitly supported. Other methods are passed on to curl optimistically. (default "POST")
32+
--no-curl Forces the use of the built-in internal http request instead of curl.
33+
-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.
34+
--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.
35+
-I, --proto-dir string Uses the specified directory to find the proto-file. (default "/proto")
36+
-f, --proto-file string Uses the specified file path to find the Protobuf definition of the message types within 'proto-dir' (relative file path).
37+
--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.
38+
--protoc-path string Uses the given path to invoke protoc instead of searching for protoc in PATH. Also activates --protoc.
39+
-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'.
40+
-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
41+
-o, --response-type string The Protobuf response type. See -i <request-type>. Overrides --decode-raw. If not set, then --decode-raw is used.
42+
-q, --show-output-only Suppresses all output except response Protobuf as text. Overrides and deactivates -v and -D. Errors are still printed to stderr.
43+
-s, --silent Suppresses all output on stdout. Overrides and deactivates -v, -D and -q. Errors are still printed to stderr.
44+
-u, --url string Mandatory: The url to send the request to
45+
-v, --verbose Prints version and enables verbose output. Also activates -D.
46+
--version version for protocurl

doc/template.README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,8 @@ How to make a release: [RELEASE.md](RELEASE.md)
203203

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

206+
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`,
207+
206208
Running the tests will download the protoc binaries on the first invocation automatically.
207209

208210
For development the `generated-local.Dockerfile` (via [generate-local.Dockerfile.sh](dev/generate-local.Dockerfile.sh)) is used.

src/flags.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package main
22

33
import (
44
"fmt"
5+
"os"
56
"strings"
67
)
78

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

88-
flags.StringVarP(&CurrentConfig.DataText, "data-text", "d", "",
89-
"The payload data in Protobuf text format or JSON. "+
90-
"It is inferred from the input as JSON if the first token is a '{'. "+
89+
flags.StringVarP(&CurrentConfig.DataText, "data-text-or-file", "d", "",
90+
"The payload data in Protobuf text format or JSON supplied as a string or a filepath (if first character is '@'). "+
91+
"The string is inferred from the input as JSON if the first token is a '{'."+
9192
"The format can be set explicitly via --in. Mandatory if request-type is provided."+
9293
"See "+GithubRepositoryLink)
9394

@@ -170,6 +171,16 @@ func propagateFlags() {
170171
}
171172
}
172173

174+
if strings.HasPrefix(CurrentConfig.DataText, "@") {
175+
filepath := CurrentConfig.DataText[1:]
176+
if CurrentConfig.Verbose {
177+
fmt.Printf("Input text will be read from file %s.\n", filepath)
178+
}
179+
file, err := os.ReadFile(filepath)
180+
PanicOnError(err)
181+
CurrentConfig.DataText = string(file) // assumes UTF-8
182+
}
183+
173184
if CurrentConfig.DataText != "" && CurrentConfig.RequestType == "" {
174185
PanicWithMessage("Non-empty data-body was provided, but no request type was given. Hence, encoding of data-body is not possible.")
175186
}

test/payloads/invalid.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
includeRe

test/payloads/payload.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"includeReason":true,
3+
"date": "2022-03-23T14:15:39Z"
4+
}

test/payloads/payload.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
includeReason: true, date: { seconds: 1648044939, nanos: 152000000 }

0 commit comments

Comments
 (0)