-
-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathfetch.bats
More file actions
91 lines (75 loc) · 2.3 KB
/
fetch.bats
File metadata and controls
91 lines (75 loc) · 2.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#!/usr/bin/env bats
load test_helper
load_bats_libs
# Define the app path from environment
setup() {
APP="${HHS_APPS_DIR}/fetch.bash"
}
# TC - 1
@test "--headers forwards each value to curl" {
run "${APP}" \
--headers 'X-Test-One: alpha, X-Test-Two: beta' GET https://jsonplaceholder.typicode.com/posts/1
assert_success
# Since jsonplaceholder does not echo headers, we only check response content
[[ "${output}" =~ userId ]] || fail "Expected response JSON missing"
}
# TC - 2
@test "--format routes the response through format_json" {
run "${APP}" \
--format --silent GET https://jsonplaceholder.typicode.com/posts/1
assert_success
assert_line --index 0 '{'
assert_line --index 1 --partial ' "userId":'
assert_line --index 2 --partial ' "id":'
assert_line --index 3 --partial ' "title":'
assert_line --index 4 --partial ' "body":'
assert_line --index 5 '}'
}
# TC - 3
@test "fails when method is missing" {
run "${APP}" https://example.com
assert_failure
assert_output --partial "fetch.bash Invalid HTTP method: HTTPS://EXAMPLE.COM"
}
# TC - 4
@test "fails when URL is missing" {
run "${APP}" GET
assert_failure
assert_output --partial "fetch.bash Missing required argument <url>"
}
# TC - 5
@test "fails when both method and url are missing" {
run "${APP}"
assert_failure
assert_output --partial "fetch.bash Missing required arguments <method> and <url>"
}
# TC - 6
@test "fails on unknown method" {
run "${APP}" INVALID https://example.com
assert_failure
assert_output --partial "fetch.bash Invalid HTTP method: INVALID"
}
# TC - 7
@test "fails on unknown flag" {
run "${APP}" --unknown GET https://example.com
assert_failure
assert_output --partial "fetch.bash Unknown option"
}
# TC - 8
@test "fails on --body without value" {
run "${APP}" -b GET https://example.com
assert_failure
assert_output --partial "fetch.bash --body requires a value."
}
# TC - 9
@test "fails on --headers without value" {
run "${APP}" -H GET https://example.com
assert_failure
assert_output --partial "fetch.bash --headers requires a value."
}
# TC - 10
@test "fails on --timeout with non-numeric value" {
run "${APP}" -t abc GET https://example.com
assert_failure
assert_output --partial "fetch.bash --timeout requires a numeric value."
}