| 210 | } |
| 211 | |
| 212 | func TestOpenAPITool_CallPOST(t *testing.T) { |
| 213 | t.Parallel() |
| 214 | |
| 215 | var receivedBody []byte |
| 216 | var receivedMethod string |
| 217 | apiServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 218 | receivedMethod = r.Method |
| 219 | receivedBody, _ = io.ReadAll(r.Body) |
| 220 | w.WriteHeader(http.StatusCreated) |
| 221 | _, _ = w.Write([]byte(`{"id":1,"name":"Fido"}`)) |
| 222 | })) |
| 223 | t.Cleanup(apiServer.Close) |
| 224 | |
| 225 | spec := `{ |
| 226 | "openapi": "3.0.0", |
| 227 | "info": { "title": "Test", "version": "1.0.0" }, |
| 228 | "servers": [{"url": "` + apiServer.URL + `"}], |
| 229 | "paths": { |
| 230 | "/pets": { |
| 231 | "post": { |
| 232 | "operationId": "createPet", |
| 233 | "summary": "Create a pet", |
| 234 | "requestBody": { |
| 235 | "content": { |
| 236 | "application/json": { |
| 237 | "schema": { |
| 238 | "type": "object", |
| 239 | "properties": { |
| 240 | "name": {"type": "string"}, |
| 241 | "tag": {"type": "string"} |
| 242 | }, |
| 243 | "required": ["name"] |
| 244 | } |
| 245 | } |
| 246 | } |
| 247 | }, |
| 248 | "responses": { "201": {"description": "created"} } |
| 249 | } |
| 250 | } |
| 251 | } |
| 252 | }` |
| 253 | |
| 254 | specServer := serveSpec(t, spec) |
| 255 | toolsList, err := newOpenAPIToolForTest(specServer.URL+"/openapi.json", nil).Tools(t.Context()) |
| 256 | require.NoError(t, err) |
| 257 | require.Len(t, toolsList, 1) |
| 258 | |
| 259 | result := callTool(t, toolsList[0], `{"body_name": "Fido", "body_tag": "dog"}`) |
| 260 | |
| 261 | assert.False(t, result.IsError) |
| 262 | assert.Equal(t, http.MethodPost, receivedMethod) |
| 263 | |
| 264 | var body map[string]any |
| 265 | require.NoError(t, json.Unmarshal(receivedBody, &body)) |
| 266 | assert.Equal(t, "Fido", body["name"]) |
| 267 | assert.Equal(t, "dog", body["tag"]) |
| 268 | } |
| 269 | |