File size: 2,882 Bytes
1477a90
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import fetchMock from '@fetch-mock/vitest'
import { beforeEach, describe, expect, it } from 'vitest'
import { Client, HTTPError, funcs } from '../src/index.js'

beforeEach(() => {
  fetchMock.mockReset()
})

function client() {
  return new Client({
    baseUrl: 'https://eu.api.konghq.com/v3',
    apiKey: 'k',
    fetch: fetchMock.fetchHandler,
  })
}

describe('void operations do not parse the response body', () => {
  it('resolves ingest of a single event on an empty 202 Accepted body', async () => {
    fetchMock.route('*', { status: 202, body: '' })
    const result = await funcs.ingestMeteringEvents(client(), {
      specversion: '1.0',
      id: 'evt-1',
      source: 'test',
      type: 'request',
      subject: 'customer-1',
    })
    expect(result.ok).toBe(true)
    expect(result.value).toBeUndefined()
  })

  it('resolves ingest of a batch of events (array body)', async () => {
    fetchMock.route('*', { status: 202, body: '' })
    const result = await funcs.ingestMeteringEvents(client(), [
      {
        specversion: '1.0',
        id: 'evt-1',
        source: 'test',
        type: 'request',
        subject: 'customer-1',
      },
      {
        specversion: '1.0',
        id: 'evt-2',
        source: 'test',
        type: 'request',
        subject: 'customer-2',
      },
    ])
    expect(result.ok).toBe(true)
    expect(result.value).toBeUndefined()
    const sent = JSON.parse(
      fetchMock.callHistory.lastCall()!.options.body as string,
    )
    expect(Array.isArray(sent)).toBe(true)
    expect(sent).toHaveLength(2)
  })

  it('resolves delete on a 204 No Content response', async () => {
    fetchMock.route('*', { status: 204 })
    const result = await funcs.deleteMeter(client(), { meterId: 'm' })
    expect(result.ok).toBe(true)
    expect(result.value).toBeUndefined()
  })

  it('still rejects a void operation on a non-2xx status', async () => {
    fetchMock.route('*', {
      status: 500,
      body: 'oops',
      headers: { 'Content-Type': 'text/plain' },
    })
    const result = await funcs.deleteMeter(client(), { meterId: 'm' })
    expect(result.ok).toBe(false)
    expect(result.error).toBeInstanceOf(HTTPError)
    expect((result.error as HTTPError).status).toBe(500)
  })

  it('preserves problem+json error detail on a void operation', async () => {
    fetchMock.route('*', {
      status: 404,
      body: {
        type: 't',
        title: 'Not Found',
        status: 404,
        detail: 'nope',
        instance: '/x',
      },
      headers: {
        'Content-Type': 'application/problem+json; charset=utf-8',
      },
    })
    const result = await funcs.deleteMeter(client(), { meterId: 'm' })
    expect(result.ok).toBe(false)
    const error = result.error as HTTPError
    expect(error.status).toBe(404)
    expect(error.title).toBe('Not Found')
    expect(error.message).toBe('nope')
  })
})