@@ -2,35 +2,105 @@ package cmd
22
33import (
44 "bytes"
5+ "crypto/x509"
56 _ "embed"
67 "testing"
78
89 _ "github.com/breml/rootcerts"
10+ "github.com/google/go-cmp/cmp"
911 "github.com/stretchr/testify/require"
12+ "github.com/xenos76/https-wrench/internal/requests"
1013)
1114
15+ func TestRootCmd_LoadConfig (t * testing.T ) {
16+ t .Run ("LoadConfig no config file" , func (t * testing.T ) {
17+ var mc requests.RequestsMetaConfig
18+
19+ config , err := LoadConfig ()
20+ require .NoError (t , err )
21+ require .False (t , config .Debug )
22+ require .False (t , config .Verbose )
23+ require .Empty (t , config .CaBundle )
24+
25+ if diff := cmp .Diff (mc , config .RequestsMetaConfig ); diff != "" {
26+ t .Errorf (
27+ "NewHTTPSWrenchConfig: RequestsMetaConfig mismatch (-want +got):\n %s" ,
28+ diff ,
29+ )
30+ }
31+ })
32+
33+ t .Run ("LoadConfig embedded config file" , func (t * testing.T ) {
34+ var expectedCaCertsPool * x509.CertPool
35+
36+ var expectedRequestsConfigs []requests.RequestConfig
37+
38+ cfgFile = "./embedded/config-example.yaml"
39+
40+ initConfig ()
41+
42+ config , err := LoadConfig ()
43+ require .NoError (t , err )
44+ require .False (t , config .Debug )
45+ require .True (t , config .Verbose )
46+ require .Empty (t , config .CaBundle )
47+
48+ // testing mapstructure squash/embedding of requests.RequestsMetaConfig
49+ // into HTTPSWrenchConfig
50+ require .False (t , config .RequestDebug )
51+ require .False (t , config .RequestVerbose )
52+ require .IsType (t , expectedCaCertsPool , config .CACertsPool )
53+ require .IsType (t , expectedRequestsConfigs , config .Requests )
54+
55+ // testing against the current values of the embedded config
56+ require .Equal (t , "httpBunComGet" , config .Requests [0 ].Name )
57+ require .Equal (t , "https://cat.httpbun.com:443" , config .Requests [0 ].TransportOverrideURL )
58+ })
59+ }
60+
61+ func TestRootCmd_Execute (t * testing.T ) {
62+ t .Run ("Execute empty config" , func (t * testing.T ) {
63+ cfgFile = ""
64+
65+ initConfig ()
66+
67+ _ , err := LoadConfig ()
68+
69+ require .NoError (t , err )
70+ Execute ()
71+ })
72+ }
73+
1274func TestRootCmd (t * testing.T ) {
1375 tests := []struct {
1476 name string
1577 args []string
1678 expectError bool
1779 expected []string
1880 }{
81+ // {
82+ // name: "no args",
83+ // args: []string{},
84+ // expectError: false,
85+ // expected: []string{
86+ // "HTTPS Wrench",
87+ // "Usage:",
88+ // "Available Commands:",
89+ // "Flags:",
90+ // "certinfo",
91+ // "requests",
92+ // "--config",
93+ // "--version",
94+ // "--help",
95+ // },
96+ // },
97+ //
1998 {
20- name : "no args " ,
21- args : []string {},
99+ name : "config flag valid arg " ,
100+ args : []string {"--config" , "./embedded/config-example.yaml" },
22101 expectError : false ,
23- expected : []string {
24- "HTTPS Wrench" ,
25- "Usage:" ,
26- "Available Commands:" ,
27- "Flags:" ,
28- "certinfo" ,
29- "requests" ,
30- "--config" ,
31- "--version" ,
32- "--help" ,
33- },
102+ // Unable to intercept the output
103+ expected : []string {},
34104 },
35105
36106 {
@@ -39,13 +109,7 @@ func TestRootCmd(t *testing.T) {
39109 expectError : true ,
40110 expected : []string {"flag needs an argument: --config" },
41111 },
42- {
43- name : "config flag valid arg" ,
44- args : []string {"--config" , "./embedded/config-example.yaml" },
45- expectError : false ,
46- // Unable to intercept the output
47- expected : []string {},
48- },
112+
49113 {
50114 name : "version" ,
51115 args : []string {"--version" },
0 commit comments