|
3 | 3 | mod common; |
4 | 4 |
|
5 | 5 | use crate::common::tests_dir; |
| 6 | +use rstest::rstest; |
6 | 7 | use serde_json::json; |
7 | 8 | use slumber_core::database::Database; |
8 | 9 | use wiremock::{Mock, MockServer, ResponseTemplate, matchers}; |
9 | 10 |
|
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 | +) { |
16 | 66 | 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); |
23 | 69 |
|
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); |
33 | 74 | } |
34 | 75 |
|
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 | | - |
66 | 76 | /// Test failure when a downstream request is needed but cannot be triggered |
67 | 77 | #[test] |
68 | 78 | fn test_generate_curl_trigger_error() { |
|
0 commit comments