Skip to content
This repository was archived by the owner on Mar 8, 2026. It is now read-only.

Commit 63ff480

Browse files
authored
Merge pull request #25 from gripmock/headers-support
add support headers
2 parents 3b4bfce + 351b4da commit 63ff480

File tree

3 files changed

+106
-57
lines changed

3 files changed

+106
-57
lines changed

grpctestify.sh

Lines changed: 74 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/bin/bash
22

3-
VERSION="v0.0.8"
3+
VERSION="v0.0.9"
44

55
# Color configuration
66
RED="\033[0;31m"
@@ -146,52 +146,52 @@ run_test() {
146146

147147
extract_section() {
148148
awk -v sec="$1" '
149-
function process_line(line) {
150-
in_str = 0
151-
escaped = 0
152-
res = ""
153-
for (i = 1; i <= length(line); i++) {
154-
c = substr(line, i, 1)
155-
if (escaped) {
156-
res = res c
157-
escaped = 0
158-
} else if (c == "\\") {
159-
res = res c
160-
escaped = 1
161-
} else if (c == "\"") {
162-
res = res c
163-
in_str = !in_str
164-
} else if (c == "#" && !in_str) {
165-
break
166-
} else {
167-
res = res c
168-
}
169-
}
170-
return res
171-
}
172-
$0 ~ /^[[:space:]]*#/ { next } # skip comment lines
173-
$0 ~ "^[[:space:]]*---[[:space:]]*" sec "[[:space:]]*---" {
174-
found=1
175-
next
176-
}
177-
/^[[:space:]]*---/ {
178-
found=0
179-
}
180-
found {
149+
function process_line(line) {
150+
in_str = 0
151+
escaped = 0
152+
res = ""
153+
for (i = 1; i <= length(line); i++) {
154+
c = substr(line, i, 1)
155+
if (escaped) {
156+
res = res c
157+
escaped = 0
158+
} else if (c == "\\") {
159+
res = res c
160+
escaped = 1
161+
} else if (c == "\"") {
162+
res = res c
163+
in_str = !in_str
164+
} else if (c == "#" && !in_str) {
165+
break
166+
} else {
167+
res = res c
168+
}
169+
}
170+
return res
171+
}
172+
$0 ~ /^[[:space:]]*#/ { next } # skip comment lines
173+
$0 ~ "^[[:space:]]*---[[:space:]]*" sec "[[:space:]]*---" {
174+
found=1
175+
next
176+
}
177+
/^[[:space:]]*---/ {
178+
found=0
179+
}
180+
found {
181181
# Process comments inside JSON strings
182-
processed = process_line($0)
183-
gsub(/[[:space:]]+$/, "", processed)
184-
printf "%s", processed
185-
}' "$TEST_FILE" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//'
182+
processed = process_line($0)
183+
gsub(/[[:space:]]+$/, "", processed)
184+
printf "%s\n", processed
185+
}' "$TEST_FILE" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//'
186186
}
187187

188188
ENDPOINT=$(extract_section "ENDPOINT")
189189
RESPONSE=$(extract_section "RESPONSE")
190190
ERROR=$(extract_section "ERROR")
191+
HEADERS=$(extract_section "HEADERS")
191192

192-
# Validate section coexistence
193-
if [[ (-n "$RESPONSE" && -n "$ERROR") || (-z "$RESPONSE" && -z "$ERROR") ]]; then
194-
log error "Exactly one of RESPONSE or ERROR sections must be present in $TEST_FILE"
193+
if [[ -z "$RESPONSE" && -z "$ERROR" ]]; then
194+
log error "At least one of RESPONSE or ERROR sections must be present in $TEST_FILE"
195195
return 1
196196
fi
197197

@@ -204,57 +204,74 @@ run_test() {
204204

205205
ADDRESS=$(extract_section "ADDRESS" | xargs)
206206
ADDRESS=${ADDRESS:-${DEFAULT_ADDRESS:-localhost:4770}}
207-
208207
REQUEST=$(extract_section "REQUEST")
209208

210209
log info "Configuration:"
211210
log info " ADDRESS: $ADDRESS"
212211
log info " ENDPOINT: $ENDPOINT"
212+
213+
[[ -n "$HEADERS" ]] && log info " HEADERS: $HEADERS"
213214
[[ -n "$REQUEST" ]] && log info " REQUEST: $(echo $REQUEST | jq -c .)" || log info " REQUEST: EMPTY"
214-
[[ -n "$RESPONSE" ]] && log info " RESPONSE: $(echo $RESPONSE | jq -c .)" || log info " ERROR: $(echo $ERROR | jq -c .)"
215+
[[ -n "$RESPONSE" ]] && log info " RESPONSE: $(echo $RESPONSE | jq -c .)"
216+
[[ -n "$ERROR" ]] && log info " ERROR: $(echo $ERROR | jq -c .)"
215217

216-
# Validate JSON content
217218
if [[ -n "$REQUEST" ]]; then
218-
validate_json "$REQUEST" "REQUEST"
219+
validate_json "$REQUEST" "REQUEST" || return 1
219220
fi
220-
validate_address "$ADDRESS"
221+
221222
if [[ -n "$RESPONSE" ]]; then
222-
validate_json "$RESPONSE" "RESPONSE"
223-
elif [[ -n "$ERROR" ]]; then
224-
validate_json "$ERROR" "ERROR"
223+
validate_json "$RESPONSE" "RESPONSE" || return 1
224+
fi
225+
226+
if [[ -n "$ERROR" ]]; then
227+
validate_json "$ERROR" "ERROR" || return 1
225228
fi
226229

230+
validate_address "$ADDRESS" || return 1
231+
227232
REQUEST_TMP=""
228233
if [[ -n "$REQUEST" ]]; then
229234
REQUEST_TMP=$(mktemp)
230235
echo "$REQUEST" | jq -c . >"$REQUEST_TMP"
231236
log debug "Request file: $REQUEST_TMP"
232-
if [[ "$VERBOSE" -eq 1 ]]; then
233-
log debug "Request content: $(cat "$REQUEST_TMP")"
234-
fi
237+
[[ "$VERBOSE" -eq 1 ]] && log debug "Request content: $(cat "$REQUEST_TMP")"
235238
fi
236239

237240
log info "Executing gRPC request to $ADDRESS..."
238241

239-
grpcurl_flags="-plaintext"
240-
[[ -n "$ERROR" ]] && grpcurl_flags="$grpcurl_flags -format-error"
242+
# Initialize grpcurl_flags as an array
243+
grpcurl_flags=(-plaintext)
244+
245+
# Add headers if present
246+
if [[ -n "$HEADERS" ]]; then
247+
while IFS= read -r header; do
248+
header=$(echo "$header" | sed 's/^[[:space:]]*//;s/[[:space:]]*$//')
249+
if [[ -n "$header" ]]; then
250+
grpcurl_flags+=(-H "$header")
251+
fi
252+
done <<<"$HEADERS"
253+
fi
254+
255+
# Add format-error flag if ERROR section exists
256+
[[ -n "$ERROR" ]] && grpcurl_flags+=(-format-error)
241257

258+
# Execute gRPC request
242259
temp_grpc_output=$(mktemp)
243260
temp_time=$(mktemp)
244261

245262
if [[ -n "$REQUEST_TMP" ]]; then
246-
log debug "$ grpcurl $grpcurl_flags -d @ \"$ADDRESS\" \"$ENDPOINT\" < $REQUEST_TMP"
263+
log debug "$ grpcurl ${grpcurl_flags[@]} -d @ \"$ADDRESS\" \"$ENDPOINT\" < $REQUEST_TMP"
247264
# shellcheck disable=SC2086
248265
(
249266
TIMEFORMAT='%R'
250-
{ time grpcurl $grpcurl_flags -d @ "$ADDRESS" "$ENDPOINT" <"$REQUEST_TMP" >"$temp_grpc_output" 2>&1; } 2>"$temp_time"
267+
{ time grpcurl "${grpcurl_flags[@]}" -d @ "$ADDRESS" "$ENDPOINT" <"$REQUEST_TMP" >"$temp_grpc_output" 2>&1; } 2>"$temp_time"
251268
)
252269
else
253-
log debug "$ grpcurl $grpcurl_flags \"$ADDRESS\" \"$ENDPOINT\""
270+
log debug "$ grpcurl ${grpcurl_flags[@]} \"$ADDRESS\" \"$ENDPOINT\""
254271
# shellcheck disable=SC2086
255272
(
256273
TIMEFORMAT='%R'
257-
{ time grpcurl $grpcurl_flags "$ADDRESS" "$ENDPOINT" >"$temp_grpc_output" 2>&1; } 2>"$temp_time"
274+
{ time grpcurl "${grpcurl_flags[@]}" "$ADDRESS" "$ENDPOINT" >"$temp_grpc_output" 2>&1; } 2>"$temp_time"
258275
)
259276
fi
260277

stubs/greeter.yaml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,16 @@
3434
output:
3535
data:
3636
message: Hello Charlie
37+
38+
- service: Greeter
39+
method: SayHello
40+
headers:
41+
contains:
42+
x-user: "Ivan"
43+
x-token: "123"
44+
input:
45+
equals:
46+
name: Bob
47+
output:
48+
data:
49+
message: You aren't Bob. You are Ivan.

tests/greeter_ivan.gctf

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
--- ADDRESS ---
2+
localhost:4770
3+
4+
--- ENDPOINT ---
5+
helloworld.Greeter/SayHello
6+
7+
--- HEADERS ---
8+
x-user: Ivan
9+
x-token: 123
10+
11+
--- REQUEST ---
12+
{
13+
"name": "Bob"
14+
}
15+
16+
--- RESPONSE ---
17+
{
18+
"message": "You aren't Bob. You are Ivan."
19+
}

0 commit comments

Comments
 (0)