github.hscsec.cn/aerogo/aero@v1.0.0/BodyReader_test.go (about)

     1  package aero_test
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"net/http"
     7  	"net/http/httptest"
     8  	"testing"
     9  
    10  	"github.com/aerogo/aero"
    11  	"github.com/stretchr/testify/assert"
    12  	"github.com/tdewolff/parse/buffer"
    13  )
    14  
    15  func TestBodyReader(t *testing.T) {
    16  	app := aero.New()
    17  
    18  	// Register route
    19  	app.Get("/", func(ctx *aero.Context) string {
    20  		body := ctx.Request().Body()
    21  		bodyText, _ := body.String()
    22  		return ctx.Text(bodyText)
    23  	})
    24  
    25  	// Get response
    26  	requestBody := []byte(helloWorld)
    27  	request, _ := http.NewRequest("GET", "/", buffer.NewReader(requestBody))
    28  	response := httptest.NewRecorder()
    29  	app.Handler().ServeHTTP(response, request)
    30  
    31  	// Verify response
    32  	assert.Equal(t, http.StatusOK, response.Code)
    33  	assert.Equal(t, helloWorld, response.Body.String())
    34  }
    35  
    36  func TestBodyReaderJSON(t *testing.T) {
    37  	app := aero.New()
    38  
    39  	// Register route
    40  	app.Get("/", func(ctx *aero.Context) string {
    41  		body := ctx.Request().Body()
    42  		obj, _ := body.JSONObject()
    43  		return ctx.Text(fmt.Sprint(obj["key"]))
    44  	})
    45  
    46  	// Get response
    47  	requestBody := []byte(`{"key":"value"}`)
    48  	request, _ := http.NewRequest("GET", "/", buffer.NewReader(requestBody))
    49  	response := httptest.NewRecorder()
    50  	app.Handler().ServeHTTP(response, request)
    51  
    52  	// Verify response
    53  	assert.Equal(t, http.StatusOK, response.Code)
    54  	assert.Equal(t, "value", response.Body.String())
    55  }
    56  
    57  func TestBodyReaderErrors(t *testing.T) {
    58  	app := aero.New()
    59  
    60  	app.Get("/", func(ctx *aero.Context) string {
    61  		body := ctx.Request().Body()
    62  
    63  		// JSON
    64  		bodyJSON, err := body.JSON()
    65  
    66  		assert.Error(t, err)
    67  		assert.Nil(t, bodyJSON)
    68  
    69  		// JSON object
    70  		bodyJSONObject, err := body.JSONObject()
    71  
    72  		assert.Error(t, err)
    73  		assert.Nil(t, bodyJSONObject)
    74  
    75  		return ctx.Text(helloWorld)
    76  	})
    77  
    78  	app.Get("/json-object", func(ctx *aero.Context) string {
    79  		body := ctx.Request().Body()
    80  		bodyJSONObject, err := body.JSONObject()
    81  
    82  		assert.Error(t, err)
    83  		assert.Nil(t, bodyJSONObject)
    84  
    85  		return ctx.Text(helloWorld)
    86  	})
    87  
    88  	// No body
    89  	request, _ := http.NewRequest("GET", "/", nil)
    90  	response := httptest.NewRecorder()
    91  	app.Handler().ServeHTTP(response, request)
    92  
    93  	assert.Equal(t, http.StatusOK, response.Code)
    94  
    95  	// Invalid JSON
    96  	request, _ = http.NewRequest("GET", "/", bytes.NewReader([]byte("{")))
    97  	response = httptest.NewRecorder()
    98  	app.Handler().ServeHTTP(response, request)
    99  
   100  	assert.Equal(t, http.StatusOK, response.Code)
   101  
   102  	// Not a JSON object
   103  	request, _ = http.NewRequest("GET", "/json-object", bytes.NewReader([]byte("123")))
   104  	response = httptest.NewRecorder()
   105  	app.Handler().ServeHTTP(response, request)
   106  
   107  	assert.Equal(t, http.StatusOK, response.Code)
   108  }