Skip to content

Commit

Permalink
add more test (easegress-io#666)
Browse files Browse the repository at this point in the history
* add more test
  • Loading branch information
suchen-sci committed Jun 24, 2022
1 parent 760083d commit fdce281
Show file tree
Hide file tree
Showing 9 changed files with 315 additions and 38 deletions.
16 changes: 16 additions & 0 deletions pkg/filters/builder/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,26 @@ import (
"testing"

"github.com/megaease/easegress/pkg/logger"
"github.com/stretchr/testify/assert"
)

func TestMain(m *testing.M) {
logger.InitNop()
code := m.Run()
os.Exit(code)
}

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

invalidSpec := Spec{}
err := invalidSpec.Validate()
assert.NotNil(err)

invalidSpec2 := Spec{
SourceNamespace: "DEFAULT",
Template: "fake template",
}
err = invalidSpec2.Validate()
assert.NotNil(err)
}
3 changes: 3 additions & 0 deletions pkg/filters/builder/extrafuncs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package builder

import (
"encoding/json"
"testing"

"github.com/stretchr/testify/assert"
Expand All @@ -36,9 +37,11 @@ func TestToFloat64(t *testing.T) {
assert.Equal(float64(1), toFloat64(uint64(1)))
assert.Equal(float64(1), toFloat64(uint(1)))
assert.Equal(float64(1), toFloat64(uintptr(1)))
assert.Equal(float64(123), toFloat64(json.Number("123")))
assert.Equal(float64(1), toFloat64("1"))
assert.Panics(func() { toFloat64("s") })
assert.Panics(func() { toFloat64(func() {}) })
assert.Panics(func() { toFloat64(json.Number("not a number")) })
}

func TestExtraFuncs(t *testing.T) {
Expand Down
28 changes: 28 additions & 0 deletions pkg/filters/builder/requestbuilder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -464,3 +464,31 @@ template: |
newRequestBuilder.Inherit(requestBuilder)
assert.Nil(newRequestBuilder.Status())
}

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

// get method from request
// directly set body
yml := `
sourceNamespace: request1
`
{
spec := &RequestBuilderSpec{}
yaml.Unmarshal([]byte(yml), spec)
rb := getRequestBuilder(spec)
defer rb.Close()

ctx := context.New(nil)

req1, err := http.NewRequest(http.MethodDelete, "https://www.google.com?field1=value1&field2=value2", nil)
assert.Nil(err)
setRequest(t, ctx, "request1", req1)
ctx.UseNamespace("test")

res := rb.Handle(ctx)
assert.Empty(res)
testReq := ctx.GetRequest("test").(*httpprot.Request).Std()
assert.Equal(req1, testReq)
}
}
28 changes: 28 additions & 0 deletions pkg/filters/builder/responsebuilder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package builder
import (
"io"
"net/http"
"net/http/httptest"
"strings"
"testing"

Expand Down Expand Up @@ -279,3 +280,30 @@ template: |
newResponseBuilder.Inherit(responseBuilder)
assert.Nil(newResponseBuilder.Status())
}

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

// set status code directly
yml := `
sourceNamespace: response1
`
{
spec := &ResponseBuilderSpec{}
yaml.Unmarshal([]byte(yml), spec)
rb := getResponseBuilder(spec)
defer rb.Close()

ctx := context.New(nil)
stdResp := httptest.NewRecorder().Result()
resp, err := httpprot.NewResponse(stdResp)
assert.Nil(err)
ctx.SetResponse("response1", resp)

ctx.UseNamespace("test")
res := rb.Handle(ctx)
assert.Empty(res)
testResp := ctx.GetResponse("test").(*httpprot.Response).Std()
assert.Equal(stdResp, testResp)
}
}
2 changes: 2 additions & 0 deletions pkg/object/httpserver/runtime_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,6 @@ https: false

r.Close()
time.Sleep(100 * time.Millisecond)

//
}
29 changes: 27 additions & 2 deletions pkg/object/mqttproxy/mqtt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import (
_ "github.com/megaease/easegress/pkg/filters/mqttclientauth"
"github.com/megaease/easegress/pkg/logger"
"github.com/megaease/easegress/pkg/object/pipeline"
"github.com/megaease/easegress/pkg/option"
"github.com/megaease/easegress/pkg/supervisor"
"github.com/openzipkin/zipkin-go/propagation/b3"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -1361,6 +1362,7 @@ func TestBrokerHandleConn(t *testing.T) {
}

func TestMQTTProxy(t *testing.T) {
assert := assert.New(t)
mp := MQTTProxy{}
mp.Status()

Expand All @@ -1371,8 +1373,31 @@ func TestMQTTProxy(t *testing.T) {
mp.Close()

ans, err := updatePort("https://example.com:1234", "demo.com:2345")
assert.Nil(t, err)
assert.Equal(t, "https://example.com:2345", ans)
assert.Nil(err)
assert.Equal("https://example.com:2345", ans)

yamlStr := `
name: mqtt-proxy
kind: MQTTProxy
`
super := supervisor.NewMock(option.New(), nil, sync.Map{}, sync.Map{}, nil, nil, false, nil, nil)
super.Options()
superSpec, err := super.NewSpec(yamlStr)
assert.Nil(err)
f := memberURLFunc(superSpec)
assert.NotNil(f)
assert.Panics(func() { f("egName", "mqttName") })

mapper := &mockMuxMapper{
MockFunc: func(name string) (context.Handler, bool) {
return nil, false
},
}
mp.Init(superSpec, mapper)

newmp := MQTTProxy{}
newmp.Inherit(superSpec, &mp, mapper)
newmp.Close()
}

func TestPipeline(t *testing.T) {
Expand Down
130 changes: 119 additions & 11 deletions pkg/protocols/httpprot/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,17 +77,125 @@ func TestHeader(t *testing.T) {
func TestProtocol(t *testing.T) {
assert := assert.New(t)
p := &Protocol{}
stdReq, err := http.NewRequest(http.MethodGet, "https://127.0.0.1:8080", nil)
assert.Nil(err)

req, err := p.CreateRequest(stdReq)
assert.Nil(err)
_, ok := req.(*Request)
assert.True(ok)
{
stdReq, err := http.NewRequest(http.MethodGet, "https://127.0.0.1:8080", nil)
assert.Nil(err)

stdResp := httptest.NewRecorder().Result()
resp, err := p.CreateResponse(stdResp)
assert.Nil(err)
_, ok = resp.(*Response)
assert.True(ok)
req, err := p.CreateRequest(stdReq)
assert.Nil(err)
_, ok := req.(*Request)
assert.True(ok)

stdResp := httptest.NewRecorder().Result()
resp, err := p.CreateResponse(stdResp)
assert.Nil(err)
_, ok = resp.(*Response)
assert.True(ok)
}

{
// build request
info := p.NewRequestInfo().(*requestInfo)
info.Method = http.MethodDelete
info.Headers = make(map[string][]string)
info.Headers["X-Header"] = []string{"value"}
info.URL = "https://127.0.0.1:8888"

req, err := p.BuildRequest(info)
assert.Nil(err)
httpReq := req.(*Request)
assert.Equal(http.MethodDelete, httpReq.Std().Method)
assert.Equal(info.URL, httpReq.Std().URL.String())
assert.Equal("value", httpReq.Std().Header.Get("X-Header"))
}

{
// build request with wrong info
_, err := p.BuildRequest(struct{}{})
assert.NotNil(err)
}

{
// build request with empty info
req, err := p.BuildRequest(&requestInfo{})
assert.Nil(err)
httpReq := req.(*Request)
assert.Equal(http.MethodGet, httpReq.Std().Method)
assert.Equal("/", httpReq.Std().URL.String())
}

{
// build request with invalid method
info := p.NewRequestInfo().(*requestInfo)
info.Method = "FakeMethod"

_, err := p.BuildRequest(info)
assert.NotNil(err)
}

{
// build response
info := p.NewResponseInfo().(*responseInfo)
info.StatusCode = 503
info.Headers = map[string][]string{}
info.Headers["X-Header"] = []string{"value"}

resp, err := p.BuildResponse(info)
assert.Nil(err)
httpResp := resp.(*Response)
assert.Equal(503, httpResp.Std().StatusCode)
assert.Equal("value", httpResp.Std().Header.Get("X-Header"))
}

{
// build response with invalid info
_, err := p.BuildResponse(struct{}{})
assert.NotNil(err)
}

{
// build response with zero status code
resp, err := p.BuildResponse(&responseInfo{})
assert.Nil(err)
httpResp := resp.(*Response)
assert.Equal(200, httpResp.Std().StatusCode)
}

{
// build response with invalid status code
info := p.NewResponseInfo().(*responseInfo)
info.StatusCode = 1000

_, err := p.BuildResponse(info)
assert.NotNil(err)
}
}

func TestParseYAMLBody(t *testing.T) {
assert := assert.New(t)
{
body := `
- name: 123
- name: 234
`
_, err := parseYAMLBody([]byte(body))
assert.Nil(err)
}

{
body := `
123: 123
`
_, err := parseYAMLBody([]byte(body))
assert.NotNil(err)
}

{
body := `
name: 123
`
_, err := parseYAMLBody([]byte(body))
assert.Nil(err)
}
}
57 changes: 57 additions & 0 deletions pkg/protocols/httpprot/request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,3 +208,60 @@ func TestRequest2(t *testing.T) {
assert.Equal("https", RequestScheme(stdReq))
}
}

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

{
request := getRequest(t, http.MethodDelete, "https://127.0.0.1:8888", http.NoBody)
request.FetchPayload(10000)

builderReq := request.ToBuilderRequest("DEFAULT").(*builderRequest)
assert.NotNil(builderReq)
assert.Equal(http.MethodDelete, builderReq.Method)
assert.Equal("https://127.0.0.1:8888", builderReq.URL.String())
}

{
request := getRequest(t, http.MethodGet, "https://127.0.0.1:8888", strings.NewReader(
"string",
))
request.FetchPayload(10000)

builderReq := request.ToBuilderRequest("DEFAULT").(*builderRequest)
assert.NotNil(builderReq)
assert.Equal([]byte("string"), builderReq.RawBody())
assert.Equal("string", builderReq.Body())
}

{
request := getRequest(t, http.MethodGet, "https://127.0.0.1:8888", strings.NewReader(
`{"key":"value"}`,
))
request.FetchPayload(10000)

builderReq := request.ToBuilderRequest("DEFAULT").(*builderRequest)
assert.NotNil(builderReq)
jsonBody, err := builderReq.JSONBody()
assert.Nil(err)
jsonMap := jsonBody.(map[string]interface{})
assert.Equal("value", jsonMap["key"])
}

{
request := getRequest(t, http.MethodGet, "https://127.0.0.1:8888", strings.NewReader(`
name: test
kind: Test
`,
))
request.FetchPayload(10000)

builderReq := request.ToBuilderRequest("DEFAULT").(*builderRequest)
assert.NotNil(builderReq)
yamlBody, err := builderReq.YAMLBody()
assert.Nil(err)
yamlMap := yamlBody.(map[string]interface{})
assert.Equal("test", yamlMap["name"])
assert.Equal("Test", yamlMap["kind"])
}
}
Loading

0 comments on commit fdce281

Please sign in to comment.