|
1 | | -import { assert, assertEquals, assertFalse } from "@std/assert"; |
| 1 | +import { assert, assertEquals, assertFalse, assertRejects } from "@std/assert"; |
2 | 2 | import { FetchClient } from "../FetchClient.ts"; |
3 | 3 | import { FetchClientProvider } from "../FetchClientProvider.ts"; |
4 | 4 | import { MockRegistry } from "../mocks/MockRegistry.ts"; |
@@ -84,6 +84,72 @@ Deno.test("can queryJSON with client middleware", async () => { |
84 | 84 | assertEquals(mocks.history.query.length, 1); |
85 | 85 | }); |
86 | 86 |
|
| 87 | +Deno.test("queryJSON sends string content as application/json", async () => { |
| 88 | + const mocks = new MockRegistry(); |
| 89 | + mocks.onQuery("/todos/search").reply(200, []); |
| 90 | + |
| 91 | + const client = new FetchClient(); |
| 92 | + mocks.install(client); |
| 93 | + |
| 94 | + await client.queryJSON( |
| 95 | + "https://example.com/todos/search", |
| 96 | + JSON.stringify({ completed: false }), |
| 97 | + ); |
| 98 | + |
| 99 | + const request = mocks.history.query[0]; |
| 100 | + assertEquals(request.headers.get("Content-Type"), "application/json"); |
| 101 | + assertEquals(await request.text(), '{"completed":false}'); |
| 102 | +}); |
| 103 | + |
| 104 | +Deno.test("query preserves URLSearchParams content", async () => { |
| 105 | + const mocks = new MockRegistry(); |
| 106 | + mocks.onQuery("/todos/search").reply(200, []); |
| 107 | + |
| 108 | + const client = new FetchClient(); |
| 109 | + mocks.install(client); |
| 110 | + |
| 111 | + await client.query( |
| 112 | + "https://example.com/todos/search", |
| 113 | + new URLSearchParams({ completed: "false" }), |
| 114 | + ); |
| 115 | + |
| 116 | + const request = mocks.history.query[0]; |
| 117 | + assertEquals( |
| 118 | + request.headers.get("Content-Type"), |
| 119 | + "application/x-www-form-urlencoded;charset=UTF-8", |
| 120 | + ); |
| 121 | + assertEquals(await request.text(), "completed=false"); |
| 122 | +}); |
| 123 | + |
| 124 | +Deno.test("query preserves Blob content", async () => { |
| 125 | + const mocks = new MockRegistry(); |
| 126 | + mocks.onQuery("/todos/search").reply(200, []); |
| 127 | + |
| 128 | + const client = new FetchClient(); |
| 129 | + mocks.install(client); |
| 130 | + |
| 131 | + await client.query( |
| 132 | + "https://example.com/todos/search", |
| 133 | + new Blob(["completed = false"], { type: "application/sql" }), |
| 134 | + ); |
| 135 | + |
| 136 | + const request = mocks.history.query[0]; |
| 137 | + assertEquals(request.headers.get("Content-Type"), "application/sql"); |
| 138 | + assertEquals(await request.text(), "completed = false"); |
| 139 | +}); |
| 140 | + |
| 141 | +Deno.test("query rejects requests without content", async () => { |
| 142 | + const mocks = new MockRegistry(); |
| 143 | + mocks.onQuery("/todos/search").reply(200, []); |
| 144 | + |
| 145 | + const client = new FetchClient(); |
| 146 | + mocks.install(client); |
| 147 | + |
| 148 | + await assertRejects(async () => { |
| 149 | + await client.query("https://example.com/todos/search"); |
| 150 | + }); |
| 151 | +}); |
| 152 | + |
87 | 153 | Deno.test("can postJSON with client middleware", async () => { |
88 | 154 | const mocks = new MockRegistry(); |
89 | 155 | mocks.onPost("/todos/1").reply(200, { |
|
0 commit comments