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