Files
ocis/web/packages/web-client/tests/unit/http/fetchClient.spec.ts
Matteo 73b26532fe [full-ci] chore: [OCISDEV-215] replace axios with the native fetch API in Web (#12910)
* feat(web-client): add native fetch http core

* test(web-test-helpers): replace axios mocks with fetch-shaped helpers

* refactor(web-pkg): build HttpClient on the fetch core

* refactor(web-pkg): unify ClientService interceptors on the fetch core

* refactor(web-pkg): drop axios types from useRequest

* fix(web): align error and header access with the fetch core

* refactor(web-client): move ocs onto the fetch core

* refactor(web-client): move webdav helpers onto the fetch core

* chore(web-client): regenerate graph client with typescript-fetch

* refactor(web-client): adapt graph wrappers to the fetch-based generated client

Regenerate the libre-graph client with the typescript-fetch template and
rewrite the eight facade factories against it:

- the generated APIs are classes (`new XxxApi(config)`), take a single
  request-parameters object and resolve to the payload rather than an
  envelope, so the wrappers no longer destructure `{ data }`
- every generated request is routed through `FetchClient` via
  `Configuration.fetchApi`, keeping header injection, maintenance
  detection and `HttpError`-on-non-2xx intact. Callers keep seeing
  `HttpError` with `statusCode` and `data`, never `ResponseError`
- `toInitOverrides` merges our per-request headers into the generated
  `RequestInit` instead of replacing them, which a plain object would do
  because the runtime spreads `initOverrides` shallowly
- undeclared query parameters travel on a symbol-keyed channel, since the
  runtime assembles the URL before it applies `initOverrides`

`--type-mappings=DateTime=string` keeps date-times as strings, matching
what the previous template emitted and what the app expects.

* refactor: use the generated property names for OData-annotated graph fields

The typescript-fetch template emits real JSON serializers, so annotated
fields like `@libre.graph.permissions.actions`, `@odata.id`, `@UI.Hidden`
and `@client.synchronize` are mapped to camelCase properties on the way in
and back to their wire names on the way out. The wire format is unchanged;
only the TypeScript-side names are. Reads left on the old names would have
silently resolved to `undefined`, and writes would have been dropped.

The OData `$filter` expression in FileSideBar keeps its annotated spelling,
being a query value rather than a property.

Response-only fields are now `readonly`, which the editable copies and test
fixtures built from them need to opt out of, hence `writeable()`.

* chore: drop the axios dependency

Nothing imports axios any more, so remove it from the eight manifests that
declared it and update the web-client README, whose usage examples still
instantiated an axios client.

Also applies prettier to the files touched over the course of the migration.

* fix(web-pkg): keep the axios-era surface on HttpClient

Dropping axios changed more than the transport. Four spellings that consumers
outside this repo were written against disappeared with it, and none of them had
to.

Response headers were a plain lowercased object under axios, so `headers['etag']`
was the only way to read one; a fetch `Response` exposes a `Headers`, where
`get('etag')` is. `httpHeaders` wraps the native object in a proxy that answers
both, case-insensitively, and enumerates the lowercased names — it stays a real
`Headers`, so neither spelling is a migration.

A rejected request carried its body on `error.response.data` and its status on
`error.response.status`. `HttpError` names them `data` and `statusCode`, which
this repo unifies on, but the response those shadow is reachable from outside it,
so `buildError` defines both on the response as well and keeps axios's
`Request failed with status code <n>` message. The body is read from a clone, so
callers still get an unread one.

The config `HttpClient` takes has always named the request body `data`, and web
extensions pass `data` when they issue raw WebDAV requests through
`httpAuthenticated`. Carrying the core's `body` up would have dropped those
bodies silently, so `RequestConfig` keeps `data` and `send` translates it. For
the same reason a default-responseType body that is not JSON falls back to the
raw text instead of throwing: a WebDAV multistatus answered without an explicit
`responseType: 'text'` used to arrive as a string, and a `SyntaxError` there
would turn a working request into a failing one.

`cancel()` is back, on an `AbortController` instead of a `CancelToken`. Combining
the client-wide signal with a per-request one needs a listener per request, so
`send` disposes of them once the request has settled rather than letting them
accumulate on a long-lived client.

`mockAxiosResolve` and `mockAxiosReject` stay exported from web-test-helpers as
deprecated aliases, so suites written against the axios era keep compiling.

* fix(web-client): keep read-only graph fields in request bodies

The typescript-fetch templates omit fields the spec marks `readOnly: true` from
the generated `*ToJSON` serializers. Every field of `Quota` is annotated that
way, so `updateDrive(id, { quota: { total: 500 } })` went out as
`{ quota: {} }` and quotas could no longer be changed. Seventeen other models
were affected the same way, among them `UserUpdate` and `ItemReference`.

The annotation is now stripped from the spec before generating, which is what
the axios-generated client effectively did. That also drops the `readonly`
modifiers, so the `writeable` escape hatch is no longer needed.

* fix(web-client): let graph errors keep their status and body

The generated runtime wraps whatever its `fetchApi` throws in a `FetchError`,
which buried the `HttpError` the core raises for a non-2xx response. Callers
that branch on the status saw neither `statusCode` nor `data`, so the banned
password hint on a public link never appeared and an aborted graph request
stopped looking like an abort.

Rethrowing from an `onError` middleware escapes the wrap for every graph
operation at once.

* fix(web-client): let fetch set the multipart boundary for FormData bodies

A caller-supplied `Content-Type: multipart/form-data` carries no boundary. Axios
replaced the header before sending, fetch passes it through, so the server could not
parse the body and the admin settings logo upload silently did nothing.

* fix(web): merge request headers case-insensitively, plus PR review fixes

Header names are case-insensitive, but the header layers were merged with an
object spread, where object keys are not. `Authorization` and `authorization`
both survived it, and a spec-compliant `Headers` applies a record init with
`append`, so the two were joined into `Bearer stale, Bearer fresh` instead of
the override replacing the token. The graph bridge lowercased every per-request
header name while the client-wide ones are canonically cased, which made the
collision the normal case rather than an edge one.

Merge each layer with `Headers.set()` instead, in the fetch client and in the
graph `initOverrides` bridge, which had its own copy of the same spread. The
new specs run under `@vitest-environment node`: happy-dom's `Headers` applies a
record init with `set` semantics and would hide the bug.

Also from the PR review:

- read an error body with the caller's `responseType` rather than cloning the
  response, so a `blob` caller keeps getting a Blob on `error.data`
- treat any rejection on an aborted signal as an abort, since `abort(reason)`
  rejects with the reason verbatim and only defaults to an `AbortError`
- share the maintenance-mode `onResponse` handler between the webdav client and
  `ClientService`, with their pre-existing disagreement about clearing on an
  unrelated error made explicit instead of silently unified
- pass the graph headers straight through as a `HeadersInit` and drop the
  unused `httpClient` from the graph factory options
- collapse `combineSignals` onto `AbortSignal.any`
- let an abort through `getFileContents` unwrapped
- assert maintenance detection against the real `shouldResponseTriggerMaintenance`
  instead of a mock, which had been hiding that a 500 is not a maintenance signal

* chore(web): drop leaked test title prefixes, document the maintenance-mode widening

* fix(web): keep graph fields the generated decoder used to lose

The typescript-fetch templates rebuild every response from the fields the
spec declares, which is what lets them rename the OData annotated fields.
Two consequences were unhandled.

A field the spec does not declare is dropped. oCIS returns `attributes` on
users, filled from OCIS_USER_SEARCH_DISPLAYED_ATTRIBUTES and displayed by the
sharing autocomplete, and the field is not part of the upstream spec. It is
now declared before generating, and the generation script fails if the spec
stops matching the line it patches, so the field cannot go missing silently.

A declared field the server omits is present holding `undefined`, so
`'accountEnabled' in user` no longer says whether the server sent it. Three
places read it that way: the login select in the user edit panel showed no
value at all, its watcher kept reporting unsaved changes, and sorting the user
list by login threw on `undefined.toString()`. The same went for
`'total' in quota`, which showed a personal quota of `?` for users without one.
2026-09-15 06:23:31 +02:00

460 lines
16 KiB
TypeScript

/**
* @vitest-environment node
*
* Deliberately not happy-dom. Its `Headers` constructor applies a record init with `set`
* semantics, whereas the spec — and therefore both browsers and undici — applies it with
* `append`. Every header assertion below is about that merge, so running them against a
* forgiving implementation would let a real duplicate-header bug pass.
*/
import { FetchClient } from '../../../src/http'
import { HttpError } from '../../../src/errors'
const jsonResponse = (body: unknown, init: ResponseInit = {}) =>
new Response(JSON.stringify(body), {
status: 200,
headers: { 'Content-Type': 'application/json' },
...init
})
describe('FetchClient', () => {
let fetchMock: ReturnType<typeof vi.fn>
beforeEach(() => {
fetchMock = vi.fn()
vi.stubGlobal('fetch', fetchMock)
})
const lastCall = () => fetchMock.mock.calls[0]
describe('request envelope', () => {
it('returns data, status, statusText and headers', async () => {
fetchMock.mockResolvedValue(jsonResponse({ some: 'value' }, { statusText: 'OK' }))
const result = await new FetchClient().request<{ some: string }>('https://host/foo')
expect(result.data).toEqual({ some: 'value' })
expect(result.status).toBe(200)
expect(result.statusText).toBe('OK')
expect(result.headers.get('Content-Type')).toBe('application/json')
})
it('headers also answer to bracket access, as axios headers did', async () => {
fetchMock.mockResolvedValue(jsonResponse({}, { headers: { 'Lock-Token': '<token>' } }))
const result = await new FetchClient().request('https://host/foo')
expect(result.headers['lock-token']).toBe('<token>')
expect(result.headers['Lock-Token']).toBe('<token>')
})
})
describe('throws on non-2xx', () => {
it.each([400, 404, 500, 503])('throws HttpError for %i', async (status) => {
fetchMock.mockResolvedValue(new Response('{}', { status }))
const client = new FetchClient()
await expect(client.request('https://host/foo')).rejects.toBeInstanceOf(HttpError)
})
it('sets statusCode, not status', async () => {
fetchMock.mockResolvedValue(new Response('{}', { status: 423 }))
const error: HttpError = await new FetchClient().request('https://host/foo').catch((e) => e)
expect(error.statusCode).toBe(423)
})
it('carries the parsed error body on data', async () => {
fetchMock.mockResolvedValue(
new Response(JSON.stringify({ error: { message: 'nope' } }), { status: 400 })
)
const error: HttpError = await new FetchClient().request('https://host/foo').catch((e) => e)
expect(error.data).toEqual({ error: { message: 'nope' } })
})
it('does not fail the throw path on a non-JSON error body', async () => {
fetchMock.mockResolvedValue(new Response('<html>gateway</html>', { status: 502 }))
const error: HttpError = await new FetchClient().request('https://host/foo').catch((e) => e)
expect(error.statusCode).toBe(502)
expect(error.data).toBe('<html>gateway</html>')
})
it('keeps the axios error message, which callers may match on', async () => {
fetchMock.mockResolvedValue(new Response('{}', { status: 404, statusText: 'Not Found' }))
const error: HttpError = await new FetchClient().request('https://host/foo').catch((e) => e)
expect(error.message).toBe('Request failed with status code 404')
})
it('also exposes the body and headers under their axios names on response', async () => {
fetchMock.mockResolvedValue(
new Response(JSON.stringify({ error: 'nope' }), {
status: 429,
headers: { 'Retry-After': '30' }
})
)
const error: HttpError = await new FetchClient().request('https://host/foo').catch((e) => e)
expect((error.response as Response & { data: unknown }).data).toEqual({ error: 'nope' })
expect(error.response.headers['retry-after']).toBe('30')
expect(error.response.headers.get('retry-after')).toBe('30')
})
it('reads the error body with the responseType the caller asked for', async () => {
fetchMock.mockResolvedValue(new Response('{"error":"nope"}', { status: 400 }))
const error: HttpError = await new FetchClient()
.request('https://host/foo', { responseType: 'blob' })
.catch((e) => e)
expect(error.data).toBeInstanceOf(Blob)
})
it('returns the envelope instead of throwing when throwOnError is false', async () => {
fetchMock.mockResolvedValue(new Response('{}', { status: 404 }))
const result = await new FetchClient().request('https://host/foo', {
throwOnError: false
})
expect(result.status).toBe(404)
})
})
describe('onResponse hook', () => {
it('fires for a successful response', async () => {
fetchMock.mockResolvedValue(jsonResponse({}))
const onResponse = vi.fn()
await new FetchClient({ onResponse }).request('https://host/foo')
expect(onResponse).toHaveBeenCalledWith(
expect.objectContaining({ status: 200, requestUrl: 'https://host/foo' })
)
})
it('fires for a non-2xx response before the throw', async () => {
fetchMock.mockResolvedValue(new Response('{}', { status: 503 }))
const onResponse = vi.fn()
await new FetchClient({ onResponse }).request('https://host/foo').catch(() => undefined)
expect(onResponse).toHaveBeenCalledWith(expect.objectContaining({ status: 503 }))
})
it('reports the caller URL, not the resolved response.url', async () => {
const relative = 'ocs/v2.php/apps/notifications/api/v1/notifications/sse'
fetchMock.mockResolvedValue(new Response('{}', { status: 503 }))
const onResponse = vi.fn()
await new FetchClient({ baseUrl: 'https://host/', onResponse })
.request(relative)
.catch(() => undefined)
expect(onResponse).toHaveBeenCalledWith(expect.objectContaining({ requestUrl: relative }))
})
it('reports a transport failure as status 500 with a null response, then throws', async () => {
fetchMock.mockRejectedValue(new TypeError('Failed to fetch'))
const onResponse = vi.fn()
const error = await new FetchClient({ onResponse })
.request('https://host/foo')
.catch((e) => e)
expect(onResponse).toHaveBeenCalledWith({
response: null,
status: 500,
requestUrl: 'https://host/foo'
})
expect(error).toBeInstanceOf(HttpError)
expect(error.statusCode).toBe(500)
})
it('does not invoke onResponse when the request is aborted', async () => {
const abortError = new DOMException('aborted', 'AbortError')
fetchMock.mockRejectedValue(abortError)
const onResponse = vi.fn()
await expect(new FetchClient({ onResponse }).request('https://host/foo')).rejects.toBe(
abortError
)
expect(onResponse).not.toHaveBeenCalled()
})
/**
* `AbortController.abort(reason)` rejects the fetch with that reason verbatim, so an
* aborted request is not always identifiable by the error name. Treating one as a
* transport failure would report a 500 and could trip maintenance detection.
*/
it('propagates an abort whose reason is not named AbortError', async () => {
const controller = new AbortController()
const reason = new Error('superseded by a newer request')
fetchMock.mockImplementation(() => {
controller.abort(reason)
return Promise.reject(reason)
})
const onResponse = vi.fn()
await expect(
new FetchClient({ onResponse }).request('https://host/foo', { signal: controller.signal })
).rejects.toBe(reason)
expect(onResponse).not.toHaveBeenCalled()
})
})
describe('url and params', () => {
it('joins a relative url onto baseUrl without doubling slashes', async () => {
fetchMock.mockResolvedValue(jsonResponse({}))
await new FetchClient({ baseUrl: 'https://host/' }).request('/foo')
expect(lastCall()[0]).toBe('https://host/foo')
})
it('leaves an absolute url untouched', async () => {
fetchMock.mockResolvedValue(jsonResponse({}))
await new FetchClient({ baseUrl: 'https://host/' }).request('https://other/foo')
expect(lastCall()[0]).toBe('https://other/foo')
})
it('serializes params', async () => {
fetchMock.mockResolvedValue(jsonResponse({}))
await new FetchClient().request('https://host/foo', { params: { a: '1', b: 2 } })
expect(lastCall()[0]).toBe('https://host/foo?a=1&b=2')
})
it('appends params onto a url that already has a query', async () => {
fetchMock.mockResolvedValue(jsonResponse({}))
await new FetchClient().request('https://host/foo?x=0', { params: { a: '1' } })
expect(lastCall()[0]).toBe('https://host/foo?x=0&a=1')
})
it('leaves the url unchanged for empty or absent params', async () => {
// a fresh Response per call: a body is single-use
fetchMock.mockImplementation(() => Promise.resolve(jsonResponse({})))
const client = new FetchClient()
await client.request('https://host/foo', { params: {} })
expect(lastCall()[0]).toBe('https://host/foo')
fetchMock.mockClear()
await client.request('https://host/foo')
expect(lastCall()[0]).toBe('https://host/foo')
})
})
describe('body encoding', () => {
it('JSON-stringifies a plain object and sets Content-Type', async () => {
fetchMock.mockResolvedValue(jsonResponse({}))
await new FetchClient().request('https://host/foo', {
method: 'POST',
body: { a: 1 }
})
const init = lastCall()[1]
expect(init.body).toBe('{"a":1}')
expect((init.headers as Headers).get('Content-Type')).toBe('application/json')
})
it('passes a BodyInit through untouched and sets no Content-Type', async () => {
fetchMock.mockResolvedValue(jsonResponse({}))
const form = new FormData()
await new FetchClient().request('https://host/foo', { method: 'POST', body: form })
const init = lastCall()[1]
expect(init.body).toBe(form)
expect((init.headers as Headers).get('Content-Type')).toBeNull()
})
/**
* A `multipart/form-data` header written by hand has no boundary, and fetch would send it
* verbatim, leaving the server unable to parse the body. Callers such as the logo upload
* relied on axios replacing the header, so it has to be dropped here.
*/
it('drops a boundary-less multipart Content-Type for FormData', async () => {
fetchMock.mockResolvedValue(jsonResponse({}))
await new FetchClient().request('https://host/foo', {
method: 'POST',
body: new FormData(),
headers: { 'Content-Type': 'multipart/form-data' }
})
expect((lastCall()[1].headers as Headers).get('Content-Type')).toBeNull()
})
it('keeps a multipart Content-Type that already carries a boundary', async () => {
fetchMock.mockResolvedValue(jsonResponse({}))
const contentType = 'multipart/form-data; boundary=--abc'
await new FetchClient().request('https://host/foo', {
method: 'POST',
body: new FormData(),
headers: { 'Content-Type': contentType }
})
expect((lastCall()[1].headers as Headers).get('Content-Type')).toBe(contentType)
})
})
describe('responseType', () => {
it('reads text', async () => {
fetchMock.mockResolvedValue(new Response('hello', { status: 200 }))
const result = await new FetchClient().request('https://host/foo', {
responseType: 'text'
})
expect(result.data).toBe('hello')
})
it('reads blob', async () => {
fetchMock.mockResolvedValue(new Response('hello', { status: 200 }))
const result = await new FetchClient().request<Blob>('https://host/foo', {
responseType: 'blob'
})
expect(result.data).toBeInstanceOf(Blob)
})
it('reads arraybuffer', async () => {
fetchMock.mockResolvedValue(new Response('hello', { status: 200 }))
const result = await new FetchClient().request<ArrayBuffer>('https://host/foo', {
responseType: 'arraybuffer'
})
expect(result.data).toBeInstanceOf(ArrayBuffer)
})
it('returns undefined for responseType none and for 204', async () => {
const client = new FetchClient()
fetchMock.mockResolvedValue(new Response('ignored', { status: 200 }))
expect(
(await client.request('https://host/a', { responseType: 'none' })).data
).toBeUndefined()
fetchMock.mockResolvedValue(new Response(null, { status: 204 }))
expect((await client.request('https://host/b')).data).toBeUndefined()
})
it('returns undefined for an empty json body rather than throwing', async () => {
fetchMock.mockResolvedValue(new Response('', { status: 200 }))
const result = await new FetchClient().request('https://host/foo')
expect(result.data).toBeUndefined()
})
it('falls back to the raw body when a default-responseType body is not json', async () => {
const multistatus = '<?xml version="1.0"?><d:multistatus xmlns:d="DAV:" />'
fetchMock.mockResolvedValue(new Response(multistatus, { status: 207 }))
const result = await new FetchClient().request('https://host/foo', { method: 'REPORT' })
expect(result.data).toBe(multistatus)
})
})
describe('headers', () => {
it('applies staticHeaders, then headers(), then per-request headers', async () => {
fetchMock.mockResolvedValue(jsonResponse({}))
await new FetchClient({
staticHeaders: { 'X-Static': 'a', 'X-Shared': 'static' },
headers: () => ({ 'X-Dynamic': 'b', 'X-Shared': 'dynamic' })
}).request('https://host/foo', { headers: { 'X-Shared': 'request' } })
const headers = lastCall()[1].headers as Headers
expect(headers.get('X-Static')).toBe('a')
expect(headers.get('X-Dynamic')).toBe('b')
expect(headers.get('X-Shared')).toBe('request')
})
/**
* Header names are case-insensitive, so a later layer has to replace an earlier one no
* matter how either spelled it. The graph bridge lowercases every per-request name, which
* makes the mismatch against the canonically-cased client headers the normal case rather
* than an edge one — and a spec-compliant `Headers` would join the two values with a comma,
* sending `Bearer stale, Bearer fresh` instead of overriding the token.
*/
it.each([
['authorization', 'Authorization'],
['Authorization', 'authorization'],
['X-REQUEST-ID', 'x-request-id']
])('overrides a %s client header with a per-request %s', async (clientName, requestName) => {
fetchMock.mockResolvedValue(jsonResponse({}))
await new FetchClient({ headers: () => ({ [clientName]: 'stale' }) }).request(
'https://host/foo',
{ headers: { [requestName]: 'fresh' } }
)
const headers = lastCall()[1].headers as Headers
expect(headers.get(clientName)).toBe('fresh')
expect([...headers]).toHaveLength(1)
})
it('overrides a staticHeader whose casing differs from the per-request one', async () => {
fetchMock.mockResolvedValue(jsonResponse({}))
await new FetchClient({ staticHeaders: { 'X-Requested-With': 'XMLHttpRequest' } }).request(
'https://host/foo',
{ headers: { 'x-requested-with': 'fetch' } }
)
const headers = lastCall()[1].headers as Headers
expect(headers.get('x-requested-with')).toBe('fetch')
})
it('accepts a Headers instance as per-request headers', async () => {
fetchMock.mockResolvedValue(jsonResponse({}))
await new FetchClient({ headers: () => ({ Authorization: 'stale' }) }).request(
'https://host/foo',
{ headers: new Headers({ authorization: 'fresh' }) }
)
expect((lastCall()[1].headers as Headers).get('authorization')).toBe('fresh')
})
it('evaluates headers() on every request', async () => {
// a fresh Response per call: a body is single-use
fetchMock.mockImplementation(() => Promise.resolve(jsonResponse({})))
const headers = vi.fn().mockReturnValue({})
const client = new FetchClient({ headers })
await client.request('https://host/a')
await client.request('https://host/b')
expect(headers).toHaveBeenCalledTimes(2)
})
})
it('forwards the abort signal', async () => {
fetchMock.mockResolvedValue(jsonResponse({}))
const controller = new AbortController()
await new FetchClient().request('https://host/foo', { signal: controller.signal })
expect(lastCall()[1].signal).toBe(controller.signal)
})
})