@@ -2,9 +2,11 @@ package echo
2
2
3
3
import (
4
4
"bytes"
5
+ "crypto/tls"
5
6
"encoding/json"
6
7
"encoding/xml"
7
8
"errors"
9
+ "fmt"
8
10
"io"
9
11
"mime/multipart"
10
12
"net/http"
@@ -72,6 +74,21 @@ func (t *Template) Render(w io.Writer, name string, data interface{}, c Context)
72
74
return t .templates .ExecuteTemplate (w , name , data )
73
75
}
74
76
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
+
75
92
func TestContext (t * testing.T ) {
76
93
e := New ()
77
94
req := httptest .NewRequest (http .MethodPost , "/" , strings .NewReader (userJSON ))
@@ -184,6 +201,12 @@ func TestContext(t *testing.T) {
184
201
err = c .XML (http .StatusOK , make (chan bool ))
185
202
assert .Error (err )
186
203
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
+
187
210
// XMLPretty
188
211
rec = httptest .NewRecorder ()
189
212
c = e .NewContext (req , rec ).(* context )
@@ -431,6 +454,7 @@ func TestContextPathParam(t *testing.T) {
431
454
432
455
// Param
433
456
testify .Equal (t , "501" , c .Param ("fid" ))
457
+ testify .Equal (t , "" , c .Param ("undefined" ))
434
458
}
435
459
436
460
func TestContextFormValue (t * testing.T ) {
@@ -455,6 +479,14 @@ func TestContextFormValue(t *testing.T) {
455
479
"email" : []string {"jon@labstack.com" },
456
480
}, params )
457
481
}
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 )
458
490
}
459
491
460
492
func TestContextQueryParam (t * testing.T ) {
@@ -555,6 +587,232 @@ func TestContextHandler(t *testing.T) {
555
587
})
556
588
c := e .NewContext (nil , nil )
557
589
r .Find (http .MethodGet , "/handler" , c )
558
- c .Handler ()(c )
590
+ err := c .Handler ()(c )
559
591
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
+ }
560
818
}
0 commit comments