Skip to content

Commit be919e8

Browse files
andreiavrammsdvishr
authored andcommitted
Increase Context tests coverage (#1279)
* Test context path. * Test context handler. * Test handler error. * Text context validate. * Test context query string. * Test context undefined param. * Test context request. * Test context scheme. * Test context is websocket. * Test context multipart form params with error. * Text context bind. * Text context logger. * Text context xml response write error. * Test context real ip.
1 parent 5aec1b2 commit be919e8

File tree

1 file changed

+259
-1
lines changed

1 file changed

+259
-1
lines changed

context_test.go

Lines changed: 259 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@ package echo
22

33
import (
44
"bytes"
5+
"crypto/tls"
56
"encoding/json"
67
"encoding/xml"
78
"errors"
9+
"fmt"
810
"io"
911
"mime/multipart"
1012
"net/http"
@@ -72,6 +74,21 @@ func (t *Template) Render(w io.Writer, name string, data interface{}, c Context)
7274
return t.templates.ExecuteTemplate(w, name, data)
7375
}
7476

77+
type responseWriterErr struct {
78+
}
79+
80+
func (responseWriterErr) Header() http.Header {
81+
return http.Header{}
82+
}
83+
84+
func (responseWriterErr) Write([]byte) (int, error) {
85+
return 0, errors.New("err")
86+
}
87+
88+
func (responseWriterErr) WriteHeader(statusCode int) {
89+
90+
}
91+
7592
func TestContext(t *testing.T) {
7693
e := New()
7794
req := httptest.NewRequest(http.MethodPost, "/", strings.NewReader(userJSON))
@@ -184,6 +201,12 @@ func TestContext(t *testing.T) {
184201
err = c.XML(http.StatusOK, make(chan bool))
185202
assert.Error(err)
186203

204+
// XML response write error
205+
c = e.NewContext(req, rec).(*context)
206+
c.response.Writer = responseWriterErr{}
207+
err = c.XML(0, 0)
208+
testify.Error(t, err)
209+
187210
// XMLPretty
188211
rec = httptest.NewRecorder()
189212
c = e.NewContext(req, rec).(*context)
@@ -431,6 +454,7 @@ func TestContextPathParam(t *testing.T) {
431454

432455
// Param
433456
testify.Equal(t, "501", c.Param("fid"))
457+
testify.Equal(t, "", c.Param("undefined"))
434458
}
435459

436460
func TestContextFormValue(t *testing.T) {
@@ -455,6 +479,14 @@ func TestContextFormValue(t *testing.T) {
455479
"email": []string{"jon@labstack.com"},
456480
}, params)
457481
}
482+
483+
// Multipart FormParams error
484+
req = httptest.NewRequest(http.MethodPost, "/", strings.NewReader(f.Encode()))
485+
req.Header.Add(HeaderContentType, MIMEMultipartForm)
486+
c = e.NewContext(req, nil)
487+
params, err = c.FormParams()
488+
testify.Nil(t, params)
489+
testify.Error(t, err)
458490
}
459491

460492
func TestContextQueryParam(t *testing.T) {
@@ -555,6 +587,232 @@ func TestContextHandler(t *testing.T) {
555587
})
556588
c := e.NewContext(nil, nil)
557589
r.Find(http.MethodGet, "/handler", c)
558-
c.Handler()(c)
590+
err := c.Handler()(c)
559591
testify.Equal(t, "handler", b.String())
592+
testify.NoError(t, err)
593+
}
594+
595+
func TestContext_SetHandler(t *testing.T) {
596+
var c Context
597+
c = new(context)
598+
599+
testify.Nil(t, c.Handler())
600+
601+
c.SetHandler(func(c Context) error {
602+
return nil
603+
})
604+
testify.NotNil(t, c.Handler())
605+
}
606+
607+
func TestContext_Path(t *testing.T) {
608+
path := "/pa/th"
609+
610+
var c Context
611+
c = new(context)
612+
613+
c.SetPath(path)
614+
testify.Equal(t, path, c.Path())
615+
}
616+
617+
type validator struct{}
618+
619+
func (*validator) Validate(i interface{}) error {
620+
return nil
621+
}
622+
623+
func TestContext_Validate(t *testing.T) {
624+
e := New()
625+
c := e.NewContext(nil, nil)
626+
627+
testify.Error(t, c.Validate(struct{}{}))
628+
629+
e.Validator = &validator{}
630+
testify.NoError(t, c.Validate(struct{}{}))
631+
}
632+
633+
func TestContext_QueryString(t *testing.T) {
634+
e := New()
635+
636+
queryString := "query=string&var=val"
637+
638+
req := httptest.NewRequest(GET, "/?"+queryString, nil)
639+
c := e.NewContext(req, nil)
640+
641+
testify.Equal(t, queryString, c.QueryString())
642+
}
643+
644+
func TestContext_Request(t *testing.T) {
645+
var c Context
646+
c = new(context)
647+
648+
testify.Nil(t, c.Request())
649+
650+
req := httptest.NewRequest(GET, "/path", nil)
651+
c.SetRequest(req)
652+
653+
testify.Equal(t, req, c.Request())
654+
}
655+
656+
func TestContext_Scheme(t *testing.T) {
657+
tests := []struct {
658+
c Context
659+
s string
660+
}{
661+
{
662+
&context{
663+
request: &http.Request{
664+
TLS: &tls.ConnectionState{},
665+
},
666+
},
667+
"https",
668+
},
669+
{
670+
&context{
671+
request: &http.Request{
672+
Header: http.Header{HeaderXForwardedProto: []string{"https"}},
673+
},
674+
},
675+
"https",
676+
},
677+
{
678+
&context{
679+
request: &http.Request{
680+
Header: http.Header{HeaderXForwardedProtocol: []string{"http"}},
681+
},
682+
},
683+
"http",
684+
},
685+
{
686+
&context{
687+
request: &http.Request{
688+
Header: http.Header{HeaderXForwardedSsl: []string{"on"}},
689+
},
690+
},
691+
"https",
692+
},
693+
{
694+
&context{
695+
request: &http.Request{
696+
Header: http.Header{HeaderXUrlScheme: []string{"https"}},
697+
},
698+
},
699+
"https",
700+
},
701+
{
702+
&context{
703+
request: &http.Request{},
704+
},
705+
"http",
706+
},
707+
}
708+
709+
for _, tt := range tests {
710+
testify.Equal(t, tt.s, tt.c.Scheme())
711+
}
712+
}
713+
714+
func TestContext_IsWebSocket(t *testing.T) {
715+
tests := []struct {
716+
c Context
717+
ws testify.BoolAssertionFunc
718+
}{
719+
{
720+
&context{
721+
request: &http.Request{
722+
Header: http.Header{HeaderUpgrade: []string{"websocket"}},
723+
},
724+
},
725+
testify.True,
726+
},
727+
{
728+
&context{
729+
request: &http.Request{
730+
Header: http.Header{HeaderUpgrade: []string{"Websocket"}},
731+
},
732+
},
733+
testify.True,
734+
},
735+
{
736+
&context{
737+
request: &http.Request{},
738+
},
739+
testify.False,
740+
},
741+
{
742+
&context{
743+
request: &http.Request{
744+
Header: http.Header{HeaderUpgrade: []string{"other"}},
745+
},
746+
},
747+
testify.False,
748+
},
749+
}
750+
751+
for i, tt := range tests {
752+
t.Run(fmt.Sprintf("test %d", i+1), func(t *testing.T) {
753+
tt.ws(t, tt.c.IsWebSocket())
754+
})
755+
}
756+
}
757+
758+
func TestContext_Bind(t *testing.T) {
759+
e := New()
760+
req := httptest.NewRequest(POST, "/", strings.NewReader(userJSON))
761+
c := e.NewContext(req, nil)
762+
763+
var u *user
764+
765+
err := c.Bind(u)
766+
testify.Error(t, err)
767+
testify.Nil(t, u)
768+
769+
req.Header.Add(HeaderContentType, MIMEApplicationJSON)
770+
err = c.Bind(&u)
771+
testify.NoError(t, err)
772+
testify.Equal(t, &user{1, "Jon Snow"}, u)
773+
}
774+
775+
func TestContext_Logger(t *testing.T) {
776+
e := New()
777+
c := e.NewContext(nil, nil)
778+
779+
testify.NotNil(t, c.Logger())
780+
}
781+
782+
func TestContext_RealIP(t *testing.T) {
783+
tests := []struct {
784+
c Context
785+
s string
786+
}{
787+
{
788+
&context{
789+
request: &http.Request{
790+
Header: http.Header{HeaderXForwardedFor: []string{"127.0.0.1, 127.0.1.1, "}},
791+
},
792+
},
793+
"127.0.0.1",
794+
},
795+
{
796+
&context{
797+
request: &http.Request{
798+
Header: http.Header{
799+
"X-Real-Ip": []string{"192.168.0.1"},
800+
},
801+
},
802+
},
803+
"192.168.0.1",
804+
},
805+
{
806+
&context{
807+
request: &http.Request{
808+
RemoteAddr: "89.89.89.89:1654",
809+
},
810+
},
811+
"89.89.89.89",
812+
},
813+
}
814+
815+
for _, tt := range tests {
816+
testify.Equal(t, tt.s, tt.c.RealIP())
817+
}
560818
}

0 commit comments

Comments
 (0)