Skip to content

Commit f31e57b

Browse files
Add more curl tests
1 parent 6551281 commit f31e57b

File tree

6 files changed

+123
-62
lines changed

6 files changed

+123
-62
lines changed

crates/cli/src/completions.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,21 @@ mod tests {
161161
#[rstest]
162162
fn test_complete_recipe(_current_dir: CurrentDirGuard) {
163163
let completions = complete(complete_recipe());
164-
assert_eq!(&completions, &["getUser", "jsonBody", "chained"]);
164+
assert_eq!(
165+
&completions,
166+
&[
167+
"getUser",
168+
"query",
169+
"headers",
170+
"authBasic",
171+
"authBearer",
172+
"textBody",
173+
"jsonBody",
174+
"fileBody",
175+
"multipart",
176+
"chained"
177+
]
178+
);
165179
}
166180

167181
/// Complete request IDs from the database

crates/cli/tests/slumber.yml

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,21 +20,59 @@ requests:
2020
method: GET
2121
url: "{{ host }}/users/{{ username }}"
2222

23+
query:
24+
method: GET
25+
url: "{{ host }}/query"
26+
query:
27+
a: 1
28+
b: [2, 3]
29+
30+
headers:
31+
method: GET
32+
url: "{{ host }}/headers"
33+
headers:
34+
Content-Type: text/plain
35+
36+
authBasic:
37+
method: GET
38+
url: "{{ host }}/headers"
39+
authentication:
40+
type: basic
41+
username: "{{ username }}"
42+
password: hunter2
43+
44+
authBearer:
45+
method: GET
46+
url: "{{ host }}/headers"
47+
authentication:
48+
type: bearer
49+
token: my-token
50+
51+
textBody:
52+
method: POST
53+
url: "{{ host }}/text"
54+
body: "This is an HTTP body"
55+
2356
jsonBody:
2457
method: POST
2558
url: "{{ host }}/json"
2659
body:
2760
type: json
2861
data: { "username": "{{ username }}", "name": "Frederick Smidgen" }
2962

63+
fileBody:
64+
method: POST
65+
url: "{{ host }}/file"
66+
body: "{{ file('test_data/data.json') }}"
67+
3068
multipart:
3169
method: POST
3270
url: "{{ host }}/multipart"
3371
body:
3472
type: form_multipart
3573
data:
3674
username: "{{ username }}"
37-
file: "{{ file('data.json') }}"
75+
file: "{{ file('test_data/data.json') }}"
3876

3977
chained:
4078
method: GET

crates/cli/tests/test_data

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

crates/cli/tests/test_generate.rs

Lines changed: 62 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -3,66 +3,76 @@
33
mod common;
44

55
use crate::common::tests_dir;
6+
use rstest::rstest;
67
use serde_json::json;
78
use slumber_core::database::Database;
89
use wiremock::{Mock, MockServer, ResponseTemplate, matchers};
910

10-
/// Test generating a curl command with:
11-
/// - URL
12-
/// - Query params
13-
/// - Headers
14-
#[test]
15-
fn test_generate_curl() {
11+
/// Test generating a curl command with a variety of request components
12+
#[rstest]
13+
#[case::url(
14+
&["getUser"],
15+
"curl -XGET --url 'http://server/users/username1'\n",
16+
)]
17+
#[case::query(
18+
&["query"],
19+
"curl -XGET --url 'http://server/query?a=1&b=2&b=3'\n",
20+
)]
21+
#[case::headers(
22+
&["headers"],
23+
"curl -XGET --url 'http://server/headers' \
24+
--header 'content-type: text/plain'\n",
25+
)]
26+
#[case::authentication_basic(
27+
&["authBasic"],
28+
"curl -XGET --url 'http://server/headers' --user 'username1:hunter2'\n",
29+
)]
30+
#[case::authentication_bearer(
31+
&["authBearer"],
32+
"curl -XGET --url 'http://server/headers' \
33+
--header 'authorization: Bearer my-token'\n",
34+
)]
35+
#[case::text(
36+
&["textBody"],
37+
"curl -XPOST --url 'http://server/text' --data 'This is an HTTP body'\n",
38+
)]
39+
#[case::json(
40+
&["jsonBody"],
41+
"curl -XPOST --url 'http://server/json' \
42+
--json '{\"username\":\"username1\",\"name\":\"Frederick Smidgen\"}'\n",
43+
)]
44+
#[case::file(
45+
&["fileBody"],
46+
"curl -XPOST --url 'http://server/file' --data '{ \"a\": 1, \"b\": 2 }'\n",
47+
)]
48+
#[case::multipart(
49+
&["multipart"],
50+
"curl -XPOST --url 'http://server/multipart' \
51+
-F 'username=username1' \
52+
-F 'file=@{ROOT}/test_data/data.json'\n"
53+
)]
54+
#[case::profile(
55+
&["getUser", "-p", "profile2"],
56+
"curl -XGET --url 'http://server/users/username2'\n",
57+
)]
58+
#[case::overrides(
59+
&["getUser", "-o", "username=username3"],
60+
"curl -XGET --url 'http://server/users/username3'\n",
61+
)]
62+
fn test_generate_curl(
63+
#[case] arguments: &[&str],
64+
#[case] expected: &'static str,
65+
) {
1666
let (mut command, _) = common::slumber();
17-
command.args(["generate", "curl", "getUser"]);
18-
command
19-
.assert()
20-
.success()
21-
.stdout("curl -XGET --url 'http://server/users/username1'\n");
22-
}
67+
command.args(["generate", "curl"]);
68+
command.args(arguments);
2369

24-
/// Make sure the profile option is reflected correctly
25-
#[test]
26-
fn test_generate_curl_profile() {
27-
let (mut command, _) = common::slumber();
28-
command.args(["generate", "curl", "getUser", "-p", "profile2"]);
29-
command
30-
.assert()
31-
.success()
32-
.stdout("curl -XGET --url 'http://server/users/username2'\n");
70+
// The expected output may contain file paths, which will be dynamic based
71+
// on the system, so we need to make the expectation dynamic too
72+
let expected = expected.replace("{ROOT}", &tests_dir().to_string_lossy());
73+
command.assert().success().stdout(expected);
3374
}
3475

35-
/// Make sure field overrides are applied correctly
36-
#[test]
37-
fn test_generate_curl_override() {
38-
let (mut command, _) = common::slumber();
39-
command.args(["generate", "curl", "getUser", "-o", "username=username3"]);
40-
command
41-
.assert()
42-
.success()
43-
.stdout("curl -XGET --url 'http://server/users/username3'\n");
44-
}
45-
46-
/// Test generating a curl command with a multipart form body, where one field
47-
/// is streamed from a file
48-
#[test]
49-
fn test_generate_curl_multipart() {
50-
let (mut command, _) = common::slumber();
51-
command.args(["generate", "curl", "multipart"]);
52-
command.assert().success().stdout(
53-
"curl -XPOST --url 'http://server/multipart' \
54-
-F 'username=username1' \
55-
-F 'file=@{ROOT}/data.json'\n"
56-
// The path is dynamic based on the system, so we need to make the
57-
// expectation dynamic too
58-
.replace("{ROOT}", &tests_dir().to_string_lossy()),
59-
);
60-
}
61-
62-
// TODO file body test
63-
// TODO text body test
64-
// TODO json body test
65-
6676
/// Test failure when a downstream request is needed but cannot be triggered
6777
#[test]
6878
fn test_generate_curl_trigger_error() {

crates/core/src/http/tests.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -339,8 +339,7 @@ Content-Disposition: form-data; name=\"user_id\"\r
339339
Content-Disposition: form-data; name=\"file\"; filename=\"data.json\"\r
340340
Content-Type: application/json\r
341341
\r
342-
{ \"a\": 1, \"b\": 2 }
343-
\r
342+
{ \"a\": 1, \"b\": 2 }\r
344343
--BOUNDARY--\r
345344
",
346345
)]
@@ -356,8 +355,7 @@ Content-Type: application/json\r
356355
"--BOUNDARY\r
357356
Content-Disposition: form-data; name=\"file\"\r
358357
\r
359-
data: { \"a\": 1, \"b\": 2 }
360-
\r
358+
data: { \"a\": 1, \"b\": 2 }\r
361359
--BOUNDARY--\r
362360
",
363361
)]

crates/template/src/tests.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ async fn test_expression(#[case] template: Template, #[case] expected: Value) {
5151
Value::Bytes(b"my name is \xc3\x28".as_slice().into(),
5252
))]
5353
// Stream gets resolved to bytes, then converted to a string
54-
#[case::stream("{{ stream() }}", "{ \"a\": 1, \"b\": 2 }\n".into())]
54+
#[case::stream("{{ stream() }}", "{ \"a\": 1, \"b\": 2 }".into())]
5555
#[tokio::test]
5656
async fn test_render_value(
5757
#[case] template: Template,
@@ -68,11 +68,11 @@ async fn test_render_value(
6868

6969
/// Render to a stream
7070
#[rstest]
71-
#[case::stream("{{ stream() }}", b"{ \"a\": 1, \"b\": 2 }\n", true)]
72-
#[case::text("text: {{ stream() }}", b"text: { \"a\": 1, \"b\": 2 }\n", false)]
71+
#[case::stream("{{ stream() }}", b"{ \"a\": 1, \"b\": 2 }", true)]
72+
#[case::text("text: {{ stream() }}", b"text: { \"a\": 1, \"b\": 2 }", false)]
7373
#[case::binary(
7474
"{{ invalid_utf8 }} {{ stream() }}",
75-
b"\xc3\x28 { \"a\": 1, \"b\": 2 }\n",
75+
b"\xc3\x28 { \"a\": 1, \"b\": 2 }",
7676
false
7777
)]
7878
#[tokio::test]

0 commit comments

Comments
 (0)