Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update request handlers and add tests #237

Merged
merged 2 commits into from
Apr 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/nervous-apricots-crash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@tbdex/http-server": patch
---

Fixes http-server GET /exchange/\* responses
5 changes: 4 additions & 1 deletion packages/http-server/src/request-handlers/get-exchange.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { ExchangesApi, GetExchangeCallback } from '../types.js'
import { TbdexHttpClient } from '@tbdex/http-client'
import { Message } from '@tbdex/protocol'
import { Request, Response } from 'express'

type GetExchangeOpts = {
Expand Down Expand Up @@ -49,5 +50,7 @@ export async function getExchange(request: Request, response: Response, opts: Ge
const _result = await callback({ request, response })
}

response.status(200).json({ data: exchange })
const data: Message[] = exchange.messages

response.status(200).json({ data })
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { ExchangesApi, GetExchangesCallback, GetExchangesFilter } from '../types.js'

import { TbdexHttpClient } from '@tbdex/http-client'
import { Message } from '@tbdex/protocol'
import { Request, Response } from 'express'

type GetExchangesOpts = {
Expand Down Expand Up @@ -53,5 +54,7 @@ export async function getExchanges(request: Request, response: Response, opts: G
const _result = await callback({ request, response }, queryParams)
}

response.status(200).json({ data: exchanges })
const data: Message[][] = exchanges.map(exchange => exchange.messages)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the response body in the spec is an array of id's, whereas this is a 2d array of exchange messages, right? are we not doing that because of other constraints (like because that would require more broad sweeping changes?) https://github.com/TBD54566975/tbdex/tree/main/specs/http-api#response-4

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hm good thought! the client currently expects Message[][] so best to make a separate PR that updates both client and server according to new spec changes.

can fast follow with that but prob good to at least get this fix up so its at min bug free 🐛


response.status(200).json({ data })
}
22 changes: 22 additions & 0 deletions packages/http-server/tests/get-exchange.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,4 +139,26 @@ describe('GET /exchanges', () => {
expect(callbackSpy.callCount).to.eq(1)
// TODO: Check what arguments are passed to callback after we finalize its behavior
})

it(`returns Exchange.messages from ExchangesApi.getExchange`, async () => {
const rfq = await DevTools.createRfq({ sender: alice, receiver: pfi })
await rfq.sign(alice);
(api.exchangesApi as InMemoryExchangesApi).addMessage(rfq)

const exchangesApiSpy = sinon.spy(api.exchangesApi, 'getExchange')

const requestToken = await TbdexHttpClient.generateRequestToken({ requesterDid: alice, pfiDid: api.pfiDid })
const exchangeId = rfq.metadata.exchangeId
const resp = await fetch(`http://localhost:8000/exchanges/${exchangeId}`, {
headers: {
'Authorization': `Bearer ${requestToken}`
}
})

const data = await resp.json()
const rfqJson = rfq.toJSON()
expect(data).to.deep.equal({ data: [ rfqJson ] })

exchangesApiSpy.restore()
})
})
23 changes: 23 additions & 0 deletions packages/http-server/tests/get-exchanges.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,4 +160,27 @@ describe('GET /exchanges', () => {
expect(callbackSpy.callCount).to.eq(1)
// TODO: Check what arguments are passed to callback after we finalize its behavior
})

it(`returns an array of Exchange.messages from ExchangesApi.getExchanges`, async () => {
const aliceDid = await DidJwk.create()
const pfiDid = await DidJwk.create()
const rfq = await DevTools.createRfq({ sender: aliceDid, receiver: pfiDid })
await rfq.sign(aliceDid);
(api.exchangesApi as InMemoryExchangesApi).addMessage(rfq)

const exchangesApiSpy = sinon.spy(api.exchangesApi, 'getExchanges')

const requestToken = await TbdexHttpClient.generateRequestToken({ requesterDid: aliceDid, pfiDid: api.pfiDid })
const resp = await fetch(`http://localhost:8000/exchanges`, {
headers: {
'Authorization': `Bearer ${requestToken}`
}
})

const data: any = await resp.json()
const rfqJson = rfq.toJSON()
expect(data).to.deep.equal({ data: [[ rfqJson ]] })

exchangesApiSpy.restore()
})
})
Loading