|
| 1 | +import { describe, expect, it } from "vitest"; |
| 2 | +import { formatBatchRequest, toBatchSubRequestUrl } from "../src/client/batch-request"; |
| 3 | + |
| 4 | +describe("toBatchSubRequestUrl", () => { |
| 5 | + it("strips /otto/ prefix and .fmp12 extension", () => { |
| 6 | + const result = toBatchSubRequestUrl( |
| 7 | + "https://host.example.com/otto/fmi/odata/v4/GMT_Web.fmp12/bookings?$top=1&$select=_GMTNum", |
| 8 | + ); |
| 9 | + expect(result).toBe("/fmi/odata/v4/GMT_Web/bookings?$top=1&$select=_GMTNum"); |
| 10 | + }); |
| 11 | + |
| 12 | + it("strips .fmp12 extension without /otto/ prefix", () => { |
| 13 | + const result = toBatchSubRequestUrl("https://host.example.com/fmi/odata/v4/GMT_Web.fmp12/bookings"); |
| 14 | + expect(result).toBe("/fmi/odata/v4/GMT_Web/bookings"); |
| 15 | + }); |
| 16 | + |
| 17 | + it("handles URLs without /otto/ or .fmp12", () => { |
| 18 | + const result = toBatchSubRequestUrl("https://host.example.com/fmi/odata/v4/MyDB/contacts"); |
| 19 | + expect(result).toBe("/fmi/odata/v4/MyDB/contacts"); |
| 20 | + }); |
| 21 | + |
| 22 | + it("preserves query parameters", () => { |
| 23 | + const result = toBatchSubRequestUrl( |
| 24 | + "https://host.example.com/otto/fmi/odata/v4/MyDB.fmp12/contacts?$filter=name eq 'test'&$top=10", |
| 25 | + ); |
| 26 | + expect(result).toBe("/fmi/odata/v4/MyDB/contacts?$filter=name%20eq%20%27test%27&$top=10"); |
| 27 | + }); |
| 28 | +}); |
| 29 | + |
| 30 | +describe("formatBatchRequest sub-request URLs", () => { |
| 31 | + it("uses canonical paths without /otto/ prefix or .fmp12 in sub-requests", () => { |
| 32 | + const baseUrl = "https://host.example.com/otto/fmi/odata/v4/GMT_Web.fmp12"; |
| 33 | + const { body } = formatBatchRequest( |
| 34 | + [{ method: "GET", url: `${baseUrl}/bookings?$top=1&$select=_GMTNum` }], |
| 35 | + baseUrl, |
| 36 | + ); |
| 37 | + |
| 38 | + // The sub-request line must use the canonical path |
| 39 | + expect(body).toContain("GET /fmi/odata/v4/GMT_Web/bookings?$top=1&$select=_GMTNum HTTP/1.1"); |
| 40 | + // Must NOT contain the otto prefix or .fmp12 in the request line |
| 41 | + expect(body).not.toContain("/otto/"); |
| 42 | + expect(body).not.toContain(".fmp12"); |
| 43 | + }); |
| 44 | + |
| 45 | + it("handles relative URLs by prepending baseUrl then transforming", () => { |
| 46 | + const baseUrl = "https://host.example.com/otto/fmi/odata/v4/MyDB.fmp12"; |
| 47 | + const { body } = formatBatchRequest([{ method: "GET", url: "/contacts?$top=5" }], baseUrl); |
| 48 | + |
| 49 | + expect(body).toContain("GET /fmi/odata/v4/MyDB/contacts?$top=5 HTTP/1.1"); |
| 50 | + }); |
| 51 | +}); |
0 commit comments