|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + _ "embed" |
| 6 | + "testing" |
| 7 | + |
| 8 | + _ "github.com/breml/rootcerts" |
| 9 | + "github.com/stretchr/testify/require" |
| 10 | +) |
| 11 | + |
| 12 | +func TestRequestsCmd(t *testing.T) { |
| 13 | + tests := []struct { |
| 14 | + name string |
| 15 | + args []string |
| 16 | + expectError bool |
| 17 | + errMsgs []string |
| 18 | + expected []string |
| 19 | + }{ |
| 20 | + { |
| 21 | + name: "no args", |
| 22 | + args: []string{"requests"}, |
| 23 | + expectError: false, |
| 24 | + expected: []string{ |
| 25 | + "https-wrench requests", |
| 26 | + "Usage:", |
| 27 | + "Flags:", |
| 28 | + "Global Flags:", |
| 29 | + "--config", |
| 30 | + "--ca-bundle", |
| 31 | + "--show-sample-config", |
| 32 | + "--version", |
| 33 | + "--help", |
| 34 | + }, |
| 35 | + }, |
| 36 | + { |
| 37 | + name: "show sample config", |
| 38 | + args: []string{"requests", "--show-sample-config"}, |
| 39 | + expectError: false, |
| 40 | + expected: []string{ |
| 41 | + "https-wrench.schema.json", |
| 42 | + "requests:", |
| 43 | + "transportOverrideUrl:", |
| 44 | + "requestHeaders:", |
| 45 | + }, |
| 46 | + }, |
| 47 | + |
| 48 | + { |
| 49 | + name: "ca-bundle flag no value", |
| 50 | + args: []string{"requests", "--ca-bundle"}, |
| 51 | + expectError: true, |
| 52 | + errMsgs: []string{ |
| 53 | + "flag needs an argument: --ca-bundle", |
| 54 | + }, |
| 55 | + expected: []string{ |
| 56 | + "https-wrench requests [flags]", |
| 57 | + "--ca-bundle string", |
| 58 | + "Usage:", |
| 59 | + "--version Display the version", |
| 60 | + }, |
| 61 | + }, |
| 62 | + |
| 63 | + { |
| 64 | + name: "config flag no file", |
| 65 | + args: []string{"requests", "--config"}, |
| 66 | + expectError: true, |
| 67 | + errMsgs: []string{ |
| 68 | + "flag needs an argument: --config", |
| 69 | + }, |
| 70 | + expected: []string{ |
| 71 | + "https-wrench requests [flags]", |
| 72 | + "--ca-bundle string", |
| 73 | + "Usage:", |
| 74 | + "--version Display the version", |
| 75 | + }, |
| 76 | + }, |
| 77 | + |
| 78 | + // WARN conflicts with same test for rootCmd |
| 79 | + // { |
| 80 | + // name: "config flag file not exist", |
| 81 | + // args: []string{ |
| 82 | + // "requests", |
| 83 | + // "--config", |
| 84 | + // "/not-existent-file", |
| 85 | + // }, |
| 86 | + // expectError: false, |
| 87 | + // expected: []string{ |
| 88 | + // "Config file not found:", |
| 89 | + // "https-wrench requests [flags]", |
| 90 | + // "--ca-bundle string", |
| 91 | + // "Usage:", |
| 92 | + // "--version Display the version", |
| 93 | + // }, |
| 94 | + // }, |
| 95 | + // |
| 96 | + // WARN conflicts with same test for rootCmd |
| 97 | + // { |
| 98 | + // name: "version", |
| 99 | + // args: []string{"requests", "--version"}, |
| 100 | + // expectError: false, |
| 101 | + // expected: []string{ |
| 102 | + // "https-wrench requests [flags]", |
| 103 | + // "--ca-bundle string", |
| 104 | + // "Usage:", |
| 105 | + // "--version Display the version", |
| 106 | + // }, |
| 107 | + // }, |
| 108 | + } |
| 109 | + |
| 110 | + for _, tc := range tests { |
| 111 | + tt := tc |
| 112 | + |
| 113 | + t.Run(tt.name, func(t *testing.T) { |
| 114 | + reqOut := new(bytes.Buffer) |
| 115 | + reqCmd := rootCmd |
| 116 | + reqCmd.SetOut(reqOut) |
| 117 | + reqCmd.SetErr(reqOut) |
| 118 | + reqCmd.SetArgs(tt.args) |
| 119 | + err := reqCmd.Execute() |
| 120 | + |
| 121 | + if tt.expectError { |
| 122 | + require.Error(t, err) |
| 123 | + |
| 124 | + for _, expected := range tt.errMsgs { |
| 125 | + require.ErrorContains(t, err, expected) |
| 126 | + } |
| 127 | + |
| 128 | + // return |
| 129 | + } |
| 130 | + |
| 131 | + if !tt.expectError { |
| 132 | + require.NoError(t, err) |
| 133 | + } |
| 134 | + |
| 135 | + got := reqOut.String() |
| 136 | + for _, expexted := range tt.expected { |
| 137 | + require.Contains(t, got, expexted) |
| 138 | + } |
| 139 | + }) |
| 140 | + } |
| 141 | +} |
0 commit comments