github.com/franciscocpg/up@v0.1.10/internal/proxy/request_test.go (about)

     1  package proxy
     2  
     3  import (
     4  	"encoding/json"
     5  	"io/ioutil"
     6  	"testing"
     7  
     8  	"github.com/tj/assert"
     9  )
    10  
    11  func TestNewRequest(t *testing.T) {
    12  	t.Run("GET", func(t *testing.T) {
    13  		var in Input
    14  		err := json.Unmarshal([]byte(getEvent), &in)
    15  		assert.NoError(t, err, "unmarshal")
    16  
    17  		req, err := NewRequest(&in)
    18  		assert.NoError(t, err, "new request")
    19  
    20  		assert.Equal(t, "GET", req.Method)
    21  		assert.Equal(t, "apex-ping.com", req.Host)
    22  		assert.Equal(t, "/pets/tobi", req.URL.Path)
    23  		assert.Equal(t, "format=json", req.URL.Query().Encode())
    24  		assert.Equal(t, "207.102.57.26", req.RemoteAddr)
    25  	})
    26  
    27  	t.Run("POST", func(t *testing.T) {
    28  		var in Input
    29  		err := json.Unmarshal([]byte(postEvent), &in)
    30  		assert.NoError(t, err, "unmarshal")
    31  
    32  		req, err := NewRequest(&in)
    33  		assert.NoError(t, err, "new request")
    34  
    35  		assert.Equal(t, "POST", req.Method)
    36  		assert.Equal(t, "apex-ping.com", req.Host)
    37  		assert.Equal(t, "/pets/tobi", req.URL.Path)
    38  		assert.Equal(t, "", req.URL.Query().Encode())
    39  		assert.Equal(t, "207.102.57.26", req.RemoteAddr)
    40  
    41  		b, err := ioutil.ReadAll(req.Body)
    42  		assert.NoError(t, err, "read body")
    43  
    44  		assert.Equal(t, `{ "name": "Tobi" }`, string(b))
    45  	})
    46  
    47  	t.Run("POST binary", func(t *testing.T) {
    48  		var in Input
    49  		err := json.Unmarshal([]byte(postEventBinary), &in)
    50  		assert.NoError(t, err, "unmarshal")
    51  
    52  		req, err := NewRequest(&in)
    53  		assert.NoError(t, err, "new request")
    54  
    55  		assert.Equal(t, "POST", req.Method)
    56  		assert.Equal(t, "/pets/tobi", req.URL.Path)
    57  		assert.Equal(t, "", req.URL.Query().Encode())
    58  		assert.Equal(t, "207.102.57.26", req.RemoteAddr)
    59  
    60  		b, err := ioutil.ReadAll(req.Body)
    61  		assert.NoError(t, err, "read body")
    62  
    63  		assert.Equal(t, `Hello World`, string(b))
    64  	})
    65  }