gitee.com/sasukebo/go-micro/v4@v4.7.1/api/handler/rpc/rpc_test.go (about)

     1  package rpc
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/json"
     6  	"net/http"
     7  	"reflect"
     8  	"testing"
     9  
    10  	go_api "gitee.com/sasukebo/go-micro/v4/api/proto"
    11  	"github.com/golang/protobuf/proto"
    12  )
    13  
    14  func TestRequestPayloadFromRequest(t *testing.T) {
    15  
    16  	// our test event so that we can validate serialising / deserializing of true protos works
    17  	protoEvent := go_api.Event{
    18  		Name: "Test",
    19  	}
    20  
    21  	protoBytes, err := proto.Marshal(&protoEvent)
    22  	if err != nil {
    23  		t.Fatal("Failed to marshal proto", err)
    24  	}
    25  
    26  	jsonBytes, err := json.Marshal(protoEvent)
    27  	if err != nil {
    28  		t.Fatal("Failed to marshal proto to JSON ", err)
    29  	}
    30  
    31  	type jsonUrl struct {
    32  		Key1 string `json:"key1"`
    33  		Key2 string `json:"key2"`
    34  		Name string `json:"name"`
    35  	}
    36  	jUrl := &jsonUrl{Key1: "val1", Key2: "val2", Name: "Test"}
    37  
    38  	t.Run("extracting a json from a POST request with url params", func(t *testing.T) {
    39  		r, err := http.NewRequest("POST", "http://localhost/my/path?key1=val1&key2=val2", bytes.NewReader(jsonBytes))
    40  		if err != nil {
    41  			t.Fatalf("Failed to created http.Request: %v", err)
    42  		}
    43  
    44  		extByte, err := requestPayload(r)
    45  		if err != nil {
    46  			t.Fatalf("Failed to extract payload from request: %v", err)
    47  		}
    48  		extJUrl := &jsonUrl{}
    49  		if err := json.Unmarshal(extByte, extJUrl); err != nil {
    50  			t.Fatalf("Failed to unmarshal payload from request: %v", err)
    51  		}
    52  		if !reflect.DeepEqual(extJUrl, jUrl) {
    53  			t.Fatalf("Expected %v and %v to match", extJUrl, jUrl)
    54  		}
    55  	})
    56  
    57  	t.Run("extracting a proto from a POST request", func(t *testing.T) {
    58  		r, err := http.NewRequest("POST", "http://localhost/my/path", bytes.NewReader(protoBytes))
    59  		if err != nil {
    60  			t.Fatalf("Failed to created http.Request: %v", err)
    61  		}
    62  
    63  		extByte, err := requestPayload(r)
    64  		if err != nil {
    65  			t.Fatalf("Failed to extract payload from request: %v", err)
    66  		}
    67  		if string(extByte) != string(protoBytes) {
    68  			t.Fatalf("Expected %v and %v to match", string(extByte), string(protoBytes))
    69  		}
    70  	})
    71  
    72  	t.Run("extracting JSON from a POST request", func(t *testing.T) {
    73  		r, err := http.NewRequest("POST", "http://localhost/my/path", bytes.NewReader(jsonBytes))
    74  		if err != nil {
    75  			t.Fatalf("Failed to created http.Request: %v", err)
    76  		}
    77  
    78  		extByte, err := requestPayload(r)
    79  		if err != nil {
    80  			t.Fatalf("Failed to extract payload from request: %v", err)
    81  		}
    82  		if string(extByte) != string(jsonBytes) {
    83  			t.Fatalf("Expected %v and %v to match", string(extByte), string(jsonBytes))
    84  		}
    85  	})
    86  
    87  	t.Run("extracting params from a GET request", func(t *testing.T) {
    88  
    89  		r, err := http.NewRequest("GET", "http://localhost/my/path", nil)
    90  		if err != nil {
    91  			t.Fatalf("Failed to created http.Request: %v", err)
    92  		}
    93  
    94  		q := r.URL.Query()
    95  		q.Add("name", "Test")
    96  		r.URL.RawQuery = q.Encode()
    97  
    98  		extByte, err := requestPayload(r)
    99  		if err != nil {
   100  			t.Fatalf("Failed to extract payload from request: %v", err)
   101  		}
   102  		if string(extByte) != string(jsonBytes) {
   103  			t.Fatalf("Expected %v and %v to match", string(extByte), string(jsonBytes))
   104  		}
   105  	})
   106  
   107  	t.Run("GET request with no params", func(t *testing.T) {
   108  
   109  		r, err := http.NewRequest("GET", "http://localhost/my/path", nil)
   110  		if err != nil {
   111  			t.Fatalf("Failed to created http.Request: %v", err)
   112  		}
   113  
   114  		extByte, err := requestPayload(r)
   115  		if err != nil {
   116  			t.Fatalf("Failed to extract payload from request: %v", err)
   117  		}
   118  		if string(extByte) != "" {
   119  			t.Fatalf("Expected %v and %v to match", string(extByte), "")
   120  		}
   121  	})
   122  }