@@ -20,7 +20,7 @@ const (
2020 invalidIP = "192.168.1.X"
2121 unknownCountry = "1.1.1.1"
2222 apiURI = "https://get.geojs.io/v1/ip/country/{ip}"
23- ipGeolocationHttpHeaderField = "cf-ipcountry"
23+ ipGeolocationHTTPHeaderField = "cf-ipcountry"
2424)
2525
2626func TestEmptyApi (t * testing.T ) {
@@ -35,7 +35,7 @@ func TestEmptyApi(t *testing.T) {
3535
3636 // expect error
3737 if err == nil {
38- t .Fatal ("Empty API uri accepted" )
38+ t .Fatal ("empty API uri accepted" )
3939 }
4040}
4141
@@ -51,7 +51,7 @@ func TestMissingIpInApi(t *testing.T) {
5151
5252 // expect error
5353 if err == nil {
54- t .Fatal ("Missing IP block in API uri" )
54+ t .Fatal ("missing IP block in API uri" )
5555 }
5656}
5757
@@ -65,7 +65,37 @@ func TestEmptyAllowedCountryList(t *testing.T) {
6565
6666 // expect error
6767 if err == nil {
68- t .Fatal ("Empty country list is not allowed" )
68+ t .Fatal ("empty country list is not allowed" )
69+ }
70+ }
71+
72+ func TestEmptyDeniedRequestStatusCode (t * testing.T ) {
73+ cfg := createTesterConfig ()
74+ cfg .Countries = append (cfg .Countries , "CH" )
75+
76+ ctx := context .Background ()
77+ next := http .HandlerFunc (func (rw http.ResponseWriter , req * http.Request ) {})
78+
79+ _ , err := geoblock .New (ctx , next , cfg , "GeoBlock" )
80+
81+ if err != nil {
82+ t .Fatal ("no error expected for empty denied request status code" )
83+ }
84+ }
85+
86+ func TestInvalidDeniedRequestStatusCode (t * testing.T ) {
87+ cfg := createTesterConfig ()
88+ cfg .Countries = append (cfg .Countries , "CH" )
89+ cfg .HTTPStatusCodeDeniedRequest = 1
90+
91+ ctx := context .Background ()
92+ next := http .HandlerFunc (func (rw http.ResponseWriter , req * http.Request ) {})
93+
94+ _ , err := geoblock .New (ctx , next , cfg , "GeoBlock" )
95+
96+ // expect error
97+ if err == nil {
98+ t .Fatal ("invalid denied request status code supplied" )
6999 }
70100}
71101
@@ -229,6 +259,33 @@ func TestDeniedCountry(t *testing.T) {
229259 assertStatusCode (t , recorder .Result (), http .StatusForbidden )
230260}
231261
262+ func TestCustomDeniedRequestStatusCode (t * testing.T ) {
263+ cfg := createTesterConfig ()
264+ cfg .Countries = append (cfg .Countries , "CH" )
265+ cfg .HTTPStatusCodeDeniedRequest = 418
266+
267+ ctx := context .Background ()
268+ next := http .HandlerFunc (func (rw http.ResponseWriter , req * http.Request ) {})
269+
270+ handler , err := geoblock .New (ctx , next , cfg , "GeoBlock" )
271+ if err != nil {
272+ t .Fatal (err )
273+ }
274+
275+ recorder := httptest .NewRecorder ()
276+
277+ req , err := http .NewRequestWithContext (ctx , http .MethodGet , "http://localhost" , nil )
278+ if err != nil {
279+ t .Fatal (err )
280+ }
281+
282+ req .Header .Add (xForwardedFor , caExampleIP )
283+
284+ handler .ServeHTTP (recorder , req )
285+
286+ assertStatusCode (t , recorder .Result (), http .StatusTeapot )
287+ }
288+
232289func TestAllowBlacklistMode (t * testing.T ) {
233290 cfg := createTesterConfig ()
234291 cfg .BlackListMode = true
@@ -663,7 +720,7 @@ func TestIpGeolocationHttpField(t *testing.T) {
663720 cfg := createTesterConfig ()
664721 cfg .Countries = append (cfg .Countries , "CA" )
665722 cfg .AddCountryHeader = true
666- cfg .IPGeolocationHTTPHeaderField = ipGeolocationHttpHeaderField
723+ cfg .IPGeolocationHTTPHeaderField = ipGeolocationHTTPHeaderField
667724
668725 ctx := context .Background ()
669726 next := http .HandlerFunc (func (rw http.ResponseWriter , req * http.Request ) {})
@@ -681,7 +738,7 @@ func TestIpGeolocationHttpField(t *testing.T) {
681738 }
682739
683740 req .Header .Add (xForwardedFor , caExampleIP )
684- req .Header .Add (ipGeolocationHttpHeaderField , "CA" )
741+ req .Header .Add (ipGeolocationHTTPHeaderField , "CA" )
685742
686743 handler .ServeHTTP (recorder , req )
687744
@@ -698,7 +755,7 @@ func TestIpGeolocationHttpFieldContentInvalid(t *testing.T) {
698755 cfg := createTesterConfig ()
699756 cfg .API = apiStub .URL + "/{ip}"
700757 cfg .Countries = append (cfg .Countries , "CA" )
701- cfg .IPGeolocationHTTPHeaderField = ipGeolocationHttpHeaderField
758+ cfg .IPGeolocationHTTPHeaderField = ipGeolocationHTTPHeaderField
702759
703760 ctx := context .Background ()
704761 next := http .HandlerFunc (func (rw http.ResponseWriter , req * http.Request ) {})
@@ -716,7 +773,7 @@ func TestIpGeolocationHttpFieldContentInvalid(t *testing.T) {
716773 }
717774
718775 req .Header .Add (xForwardedFor , caExampleIP )
719- req .Header .Add (ipGeolocationHttpHeaderField , "" )
776+ req .Header .Add (ipGeolocationHTTPHeaderField , "" )
720777
721778 handler .ServeHTTP (recorder , req )
722779
@@ -745,19 +802,27 @@ type CountryCodeHandler struct {
745802
746803func (h * CountryCodeHandler ) ServeHTTP (w http.ResponseWriter , r * http.Request ) {
747804 w .WriteHeader (http .StatusOK )
748- w .Write ([]byte (h .ResponseCountryCode ))
805+
806+ _ , err := w .Write ([]byte (h .ResponseCountryCode ))
807+ if err != nil {
808+ fmt .Println ("Error on write" )
809+ }
749810}
750811
751812func apiHandlerInvalid (w http.ResponseWriter , r * http.Request ) {
752813 fmt .Fprintf (w , "Invalid Response" )
753814}
754815
755816func apiTimeout (w http.ResponseWriter , r * http.Request ) {
756- var result = ``
757817 // Add waiting time for response
758818 time .Sleep (20 * time .Millisecond )
819+
759820 w .WriteHeader (http .StatusOK )
760- w .Write ([]byte (result ))
821+
822+ _ , err := w .Write ([]byte ("" ))
823+ if err != nil {
824+ fmt .Println ("Error on write" )
825+ }
761826}
762827
763828func createTesterConfig () * geoblock.Config {
0 commit comments