|
| 1 | +import { Then, When, World } from '@cucumber/cucumber' |
| 2 | +import axios, { AxiosResponse } from 'axios' |
| 3 | +import chai from 'chai' |
| 4 | + |
| 5 | +import packageJson from '../../../../package.json' |
| 6 | +import { createSettings } from '../../../../src/factories/settings-factory' |
| 7 | + |
| 8 | +chai.use(require('sinon-chai')) |
| 9 | +const { expect } = chai |
| 10 | + |
| 11 | +const BASE_URL = 'http://localhost:18808' |
| 12 | + |
| 13 | +When('a client requests the relay information document', async function(this: World<Record<string, any>>) { |
| 14 | + const response: AxiosResponse = await axios.get(BASE_URL, { |
| 15 | + headers: { Accept: 'application/nostr+json' }, |
| 16 | + validateStatus: () => true, |
| 17 | + }) |
| 18 | + this.parameters.httpResponse = response |
| 19 | +}) |
| 20 | + |
| 21 | +When('a client requests the root path with Accept header {string}', async function( |
| 22 | + this: World<Record<string, any>>, |
| 23 | + acceptHeader: string, |
| 24 | +) { |
| 25 | + const response: AxiosResponse = await axios.get(BASE_URL, { |
| 26 | + headers: { Accept: acceptHeader }, |
| 27 | + validateStatus: () => true, |
| 28 | + }) |
| 29 | + this.parameters.httpResponse = response |
| 30 | +}) |
| 31 | + |
| 32 | +Then('the response status is {int}', function(this: World<Record<string, any>>, status: number) { |
| 33 | + expect(this.parameters.httpResponse.status).to.equal(status) |
| 34 | +}) |
| 35 | + |
| 36 | +Then('the response Content-Type includes {string}', function( |
| 37 | + this: World<Record<string, any>>, |
| 38 | + contentType: string, |
| 39 | +) { |
| 40 | + expect(this.parameters.httpResponse.headers['content-type']).to.include(contentType) |
| 41 | +}) |
| 42 | + |
| 43 | +Then('the response Content-Type does not include {string}', function( |
| 44 | + this: World<Record<string, any>>, |
| 45 | + contentType: string, |
| 46 | +) { |
| 47 | + expect(this.parameters.httpResponse.headers['content-type']).to.not.include(contentType) |
| 48 | +}) |
| 49 | + |
| 50 | +Then('the relay information document contains the required fields', function(this: World<Record<string, any>>) { |
| 51 | + const doc = this.parameters.httpResponse.data |
| 52 | + for (const field of ['name', 'description', 'pubkey', 'supported_nips', 'software', 'version']) { |
| 53 | + expect(doc, `expected relay info doc to have field "${field}"`).to.have.property(field) |
| 54 | + } |
| 55 | +}) |
| 56 | + |
| 57 | +Then('the supported_nips field matches the NIPs declared in package.json', function(this: World<Record<string, any>>) { |
| 58 | + const doc = this.parameters.httpResponse.data |
| 59 | + expect(doc.supported_nips).to.deep.equal(packageJson.supportedNips) |
| 60 | +}) |
| 61 | + |
| 62 | +Then('the response body is not a relay information document', function(this: World<Record<string, any>>) { |
| 63 | + const body = this.parameters.httpResponse.data |
| 64 | + const isRelayInfoDoc = typeof body === 'object' && body !== null && 'supported_nips' in body |
| 65 | + expect(isRelayInfoDoc).to.equal(false) |
| 66 | +}) |
| 67 | + |
| 68 | +Then('the limitation object contains a max_filters field', function(this: World<Record<string, any>>) { |
| 69 | + const doc = this.parameters.httpResponse.data |
| 70 | + const expectedMaxFilters = createSettings().limits?.client?.subscription?.maxFilters |
| 71 | + expect(doc.limitation.max_filters).to.equal(expectedMaxFilters) |
| 72 | +}) |
0 commit comments