Skip to content

Commit

Permalink
add test
Browse files Browse the repository at this point in the history
  • Loading branch information
suchen-sci committed Jan 5, 2024
1 parent 8c5fdaf commit 3980390
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 6 deletions.
4 changes: 3 additions & 1 deletion cmd/client/commandv2/create/createhttpproxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -328,10 +328,12 @@ func (o *HTTPProxyOptions) translateRules() (routers.Rules, []*specs.PipelineSpe
return rules, pipelines
}

var handleReqHook = general.HandleRequest

// TranslateAutoCertManager translates AutoCertManagerSpec.
func (o *HTTPProxyOptions) TranslateAutoCertManager() (*specs.AutoCertManagerSpec, error) {
url := general.MakePath(general.ObjectsURL)
body, err := general.HandleRequest(http.MethodGet, url, nil)
body, err := handleReqHook(http.MethodGet, url, nil)
if err != nil {
return nil, err
}
Expand Down
102 changes: 97 additions & 5 deletions cmd/client/commandv2/create/createhttpproxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,16 +255,28 @@ func TestCreateHTTPProxyOptions(t *testing.T) {
"foo.com/bar*=http://127.0.0.1:9095",
"/bar=http://127.0.0.1:9095",
},
TLS: true,
AutoCert: true,
CaCertFile: createCert("ca.cert"),
CertFiles: []string{createCert("cert1"), createCert("cert2")},
KeyFiles: []string{createCert("key1"), createCert("key2")},
TLS: true,
AutoCert: true,
CaCertFile: createCert("ca.cert"),
CertFiles: []string{createCert("cert1"), createCert("cert2")},
KeyFiles: []string{createCert("key1"), createCert("key2")},
AutoCertEmail: "[email protected]",
AutoCertDomainName: "*.easegress.example",
AutoCertDNSProvider: []string{
"name=dnspod",
"zone=easegress.com",
"apiToken=abc",
},
}
o.Complete([]string{"test"})
err = o.Parse()
assert.Nil(err)

// auto cert
assert.Equal("dnspod", o.dnsProvider["name"])
assert.Equal("easegress.com", o.dnsProvider["zone"])
assert.Equal("abc", o.dnsProvider["apiToken"])

hs, pls := o.Translate()

// meta
Expand Down Expand Up @@ -383,3 +395,83 @@ func TestCreateHTTPProxyCmd(t *testing.T) {
err = httpProxyRun(cmd, []string{"demo"})
assert.NotNil(t, err)
}

func TestTranslateAutoCertManager(t *testing.T) {
assert := assert.New(t)

originalHook := handleReqHook
defer func() {
handleReqHook = originalHook
}()
handleReqHook = func(httpMethod string, path string, yamlBody []byte) ([]byte, error) {
return []byte("[]"), nil
}
option := &HTTPProxyOptions{
AutoCert: true,
AutoCertEmail: "[email protected]",
AutoCertDomainName: "*.easegress.example",
AutoCertDNSProvider: []string{
"name=dnspod",
"zone=easegress.com",
"apiToken=abc",
},
}
option.Complete([]string{"test"})
err := option.Parse()
assert.Nil(err)

spec, err := option.TranslateAutoCertManager()
assert.Nil(err)
assert.Equal("AutoCertManager", spec.Kind)
assert.Equal("autocertmanager", spec.Name)
assert.Equal("[email protected]", spec.Email)
assert.Equal(1, len(spec.Domains))
assert.Equal("*.easegress.example", spec.Domains[0].Name)
assert.Equal(map[string]string{
"name": "dnspod",
"zone": "easegress.com",
"apiToken": "abc",
}, spec.Domains[0].DNSProvider)

handleReqHook = func(httpMethod string, path string, yamlBody []byte) ([]byte, error) {
return []byte(`[
{
"kind": "AutoCertManager",
"name": "autocert",
"email": "[email protected]",
domains: [
{
"name": "*.easegress.org",
"dnsProvider": {
"name": "dnspod",
"zone": "easegress.org",
"apiToken": "abc"
}
}
]
}
]`), nil
}
option = &HTTPProxyOptions{
AutoCert: true,
AutoCertDomainName: "*.easegress.example",
AutoCertDNSProvider: []string{
"name=aliyun",
"zone=easegress.com",
"apiToken=abc",
},
}
option.Complete([]string{"test"})
err = option.Parse()
assert.Nil(err)

spec, err = option.TranslateAutoCertManager()
assert.Nil(err)
assert.Equal("AutoCertManager", spec.Kind)
assert.Equal("autocert", spec.Name)
assert.Equal("[email protected]", spec.Email)

assert.Equal(2, len(spec.Domains))
assert.Equal("*.easegress.org", spec.Domains[0].Name)
assert.Equal("*.easegress.example", spec.Domains[1].Name)
}

0 comments on commit 3980390

Please sign in to comment.