github.com/gocaveman/caveman@v0.0.0-20191211162744-0ddf99dbdf6e/httpapi/httpapi_test.go (about) 1 package httpapi 2 3 import ( 4 "bytes" 5 "fmt" 6 "net/http" 7 "net/http/httptest" 8 "testing" 9 10 "github.com/stretchr/testify/assert" 11 ) 12 13 type Something struct { 14 ID string `json:"id,omitempty"` 15 Name string `json:"name,omitempty"` 16 } 17 18 // func TestAPIStuff(t *testing.T) { 19 20 // var something Something 21 22 // r, _ := http.NewRequest("GET", "/api/something/123", nil) 23 // // ParseRequest(r).UseInput(&something) 24 25 // apir := NewRequest(r) 26 27 // apir.ParseJSONRPC("/api/jsonrpc", "get-something", &something) 28 // apir.ParseRESTPath("GET", "/api/something/%s", &something.ID) 29 // apir.ParseRESTPath("DELETE", "/api/something/%s", &something.ID) 30 // apir.ParseRESTObj("POST", &something, "/api/something") 31 // apir.ParseRESTObjPath("PATCH", &something, "/api/something/%s", &something.ID) 32 // apir.ParseRESTObj("GET", &something, "/api/something") // form values are implied here 33 34 // if apir.ParseJSONRPC("/api/jsonrpc", "get-something", &something) || apir.ParseRESTPath("GET", "/api/something/%s", &something.ID) { 35 36 // if apir.Err != nil { 37 // apir.WriteCodeErr() 38 // return 39 // } 40 41 // // do stuff with apir and something 42 43 // // we can also use Fill to copy to a db object we selected 44 // apir.Fill(obj) 45 46 // apir.WriteResult() 47 48 // } 49 50 // // if apir.RPCMethod == "get-something" || r.Method == "GET" && webutil.PathParse(r.URL.Path, "/api/something/%s", &something.ID) { 51 // // } 52 53 // } 54 55 func TestParseJSONRPC2_GET(t *testing.T) { 56 57 assert := assert.New(t) 58 59 hr, _ := http.NewRequest("GET", `/api/jsonrpc?jsonrpc=2.0&method=get-something&id=987¶ms={"id":"123"}`, nil) 60 ar := NewRequest(hr) 61 62 var something Something 63 64 assert.False(ar.ParseJSONRPC2("/wrong-endpoint", "get-something", &something, true)) 65 assert.False(ar.ParseJSONRPC2("/api/jsonrpc", "wrong-method", &something, true)) 66 assert.True(ar.ParseJSONRPC2("/api/jsonrpc", "get-something", &something, true)) 67 assert.Equal("123", something.ID) 68 69 } 70 71 func TestParseJSONRPC2_POST(t *testing.T) { 72 73 assert := assert.New(t) 74 75 body := []byte(`{"jsonrpc":"2.0","method":"get-something","id":"987","params":{"id":"123"}}`) 76 hr, _ := http.NewRequest("POST", `/api/jsonrpc`, bytes.NewReader(body)) 77 hr.Header.Set("content-type", "application/json") 78 ar := NewRequest(hr) 79 80 var something Something 81 82 assert.False(ar.ParseJSONRPC2("/wrong-endpoint", "get-something", &something, true)) 83 assert.False(ar.ParseJSONRPC2("/api/jsonrpc", "wrong-method", &something, true)) 84 assert.True(ar.ParseJSONRPC2("/api/jsonrpc", "get-something", &something, true)) 85 assert.Equal("123", something.ID) 86 87 } 88 89 func TestJSONRPC2_Write(t *testing.T) { 90 91 assert := assert.New(t) 92 93 body := []byte(`{"jsonrpc":"2.0","method":"get-something","id":"987","params":{"id":"123"}}`) 94 hr, _ := http.NewRequest("POST", `/api/jsonrpc`, bytes.NewReader(body)) 95 hr.Header.Set("content-type", "application/json") 96 ar := NewRequest(hr) 97 98 var something Something 99 100 assert.True(ar.ParseJSONRPC2("/api/jsonrpc", "get-something", &something, true)) 101 assert.NoError(ar.Err) 102 103 wrec := httptest.NewRecorder() 104 assert.NoError(ar.WriteResult(wrec, 200, "this is \" the \nresult")) 105 assert.Contains(wrec.Body.String(), `"result":"this is \" the \nresult"`) 106 107 wrec = httptest.NewRecorder() 108 assert.NoError(ar.WriteCodeErr(wrec, 409, fmt.Errorf("something strange happened"))) 109 // t.Logf("Error result:\n%s", wrec.Body.String()) 110 assert.Contains(wrec.Body.String(), `"error":{"code"`) 111 112 // wrec = httptest.NewRecorder() 113 // assert.NoError(ar.WriteCodeErr(wrec, 409, &ErrorDetail{Code: 1000, Message: "Blah", Data: nil})) 114 // // t.Logf("Error result:\n%s", wrec.Body.String()) 115 // assert.Contains(wrec.Body.String(), `"error":{"code":1000,"message":"Blah"`) 116 117 } 118 119 func TestParseRESTPath(t *testing.T) { 120 121 assert := assert.New(t) 122 123 hr, _ := http.NewRequest("GET", `/api/something/123`, nil) 124 ar := NewRequest(hr) 125 126 var something Something 127 128 assert.True(ar.ParseRESTPath("GET", "/api/something/%s", &something.ID)) 129 assert.Equal("123", something.ID) 130 131 } 132 133 func TestParseRESTObj(t *testing.T) { 134 135 assert := assert.New(t) 136 137 // try as JSON 138 something := Something{} 139 hr, _ := http.NewRequest("POST", `/api/something`, bytes.NewReader([]byte(`{"id":"id1","name":"name1"}`))) 140 hr.Header.Set("content-type", "application/json") 141 ar := NewRequest(hr) 142 143 assert.True(ar.ParseRESTObj("POST", &something, "/api/something")) 144 assert.Equal("id1", something.ID) 145 assert.Equal("name1", something.Name) 146 147 // try again as form post 148 something = Something{} 149 hr, _ = http.NewRequest("POST", `/api/something`, bytes.NewReader([]byte(`id=id1&name=name1`))) 150 hr.Header.Set("content-type", "application/x-www-form-urlencoded") 151 ar = NewRequest(hr) 152 153 assert.True(ar.ParseRESTObj("POST", &something, "/api/something")) 154 assert.Equal("id1", something.ID) 155 assert.Equal("name1", something.Name) 156 157 } 158 159 func TestParseRESTObjPath(t *testing.T) { 160 161 assert := assert.New(t) 162 163 // try as JSON 164 something := Something{} 165 hr, _ := http.NewRequest("POST", `/api/something/123`, bytes.NewReader([]byte(`{"name":"name1"}`))) 166 hr.Header.Set("content-type", "application/json") 167 ar := NewRequest(hr) 168 169 assert.True(ar.ParseRESTObjPath("POST", &something, "/api/something/%s", &something.ID)) 170 assert.Equal("123", something.ID) 171 assert.Equal("name1", something.Name) 172 173 // try again as form post 174 something = Something{} 175 hr, _ = http.NewRequest("POST", `/api/something/123`, bytes.NewReader([]byte(`name=name1`))) 176 hr.Header.Set("content-type", "application/x-www-form-urlencoded") 177 ar = NewRequest(hr) 178 179 assert.True(ar.ParseRESTObjPath("POST", &something, "/api/something/%s", &something.ID)) 180 assert.Equal("123", something.ID) 181 assert.Equal("name1", something.Name) 182 183 }