github.com/System-Glitch/goyave/v3@v3.6.1-0.20210226143142-ac2fe42ee80e/native_handler_test.go (about)

     1  package goyave
     2  
     3  import (
     4  	"io/ioutil"
     5  	"net/http"
     6  	"net/http/httptest"
     7  	"strings"
     8  	"testing"
     9  )
    10  
    11  type NativeHandlerTestSuite struct {
    12  	TestSuite
    13  }
    14  
    15  func (suite *NativeHandlerTestSuite) TestNativeHandler() {
    16  	request := &Request{
    17  		httpRequest: httptest.NewRequest("GET", "/native", nil),
    18  	}
    19  	recorder := httptest.NewRecorder()
    20  	response := newResponse(recorder, nil)
    21  	handler := NativeHandler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    22  		w.Write([]byte("Hello world"))
    23  	}))
    24  
    25  	handler(response, request)
    26  	result := recorder.Result()
    27  	body, err := ioutil.ReadAll(result.Body)
    28  	if err != nil {
    29  		panic(err)
    30  	}
    31  	result.Body.Close()
    32  	suite.Equal(200, result.StatusCode)
    33  	suite.Equal("Hello world", string(body))
    34  	suite.False(response.empty)
    35  }
    36  
    37  func (suite *NativeHandlerTestSuite) TestNativeHandlerBody() {
    38  
    39  	handler := NativeHandler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    40  		res, err := ioutil.ReadAll(r.Body)
    41  		if err != nil {
    42  			w.WriteHeader(http.StatusInternalServerError)
    43  			return
    44  		}
    45  
    46  		suite.Equal("request=content", string(res))
    47  		w.WriteHeader(http.StatusNoContent)
    48  	}))
    49  
    50  	suite.RunServer(func(router *Router) {
    51  		router.Route("POST", "/native", handler)
    52  	}, func() {
    53  		headers := map[string]string{"Content-Type": "application/x-www-form-urlencoded; param=value"}
    54  		resp, err := suite.Post("/native", headers, strings.NewReader("request=content"))
    55  		if err != nil {
    56  			panic(err)
    57  		}
    58  		defer resp.Body.Close()
    59  
    60  		suite.Equal(http.StatusNoContent, resp.StatusCode)
    61  	})
    62  }
    63  
    64  func (suite *NativeHandlerTestSuite) TestNativeHandlerBodyJSON() {
    65  
    66  	handler := NativeHandler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    67  		res, err := ioutil.ReadAll(r.Body)
    68  		if err != nil {
    69  			w.WriteHeader(http.StatusInternalServerError)
    70  			return
    71  		}
    72  
    73  		suite.Equal("{\"request\":\"content\"}", string(res))
    74  		w.WriteHeader(http.StatusNoContent)
    75  	}))
    76  
    77  	suite.RunServer(func(router *Router) {
    78  		router.Route("POST", "/native", handler)
    79  	}, func() {
    80  		headers := map[string]string{"Content-Type": "application/json"}
    81  		resp, err := suite.Post("/native", headers, strings.NewReader("{\"request\":\"content\"}"))
    82  		if err != nil {
    83  			panic(err)
    84  		}
    85  		defer resp.Body.Close()
    86  
    87  		suite.Equal(http.StatusNoContent, resp.StatusCode)
    88  	})
    89  }
    90  
    91  func (suite *NativeHandlerTestSuite) TestNativeMiddleware() {
    92  	request := &Request{
    93  		httpRequest: httptest.NewRequest("GET", "/native", nil),
    94  	}
    95  	recorder := httptest.NewRecorder()
    96  	response := newResponse(recorder, nil)
    97  	middleware := NativeMiddleware(func(next http.Handler) http.Handler {
    98  		return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    99  			w.Write([]byte("Hello world"))
   100  			next.ServeHTTP(w, r)
   101  		})
   102  	})
   103  
   104  	handlerExecuted := false
   105  	handler := func(response *Response, r *Request) {
   106  		result := recorder.Result()
   107  		body, err := ioutil.ReadAll(result.Body)
   108  		if err != nil {
   109  			panic(err)
   110  		}
   111  		result.Body.Close()
   112  		suite.Equal(200, result.StatusCode)
   113  		suite.Equal("Hello world", string(body))
   114  		suite.False(response.empty)
   115  		handlerExecuted = true
   116  	}
   117  	middleware(handler)(response, request)
   118  	suite.True(handlerExecuted)
   119  
   120  	middleware = NativeMiddleware(func(next http.Handler) http.Handler {
   121  		return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
   122  			// Blocking middleware
   123  		})
   124  	})
   125  
   126  	handlerExecuted = false
   127  	handler = func(response *Response, r *Request) {
   128  		handlerExecuted = true
   129  	}
   130  	middleware(handler)(response, request)
   131  	suite.False(handlerExecuted)
   132  }
   133  
   134  func TestNativeHandlerTestSuite(t *testing.T) {
   135  	RunTest(t, new(NativeHandlerTestSuite))
   136  }