Skip to content

Commit

Permalink
fixed request tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Egge21M committed Nov 27, 2024
1 parent a1e2d9f commit ba23325
Showing 1 changed file with 33 additions and 29 deletions.
62 changes: 33 additions & 29 deletions test/request.test.ts
Original file line number Diff line number Diff line change
@@ -1,69 +1,73 @@
import { beforeAll, beforeEach, test, describe, expect } from 'vitest';
import { beforeAll, beforeEach, test, describe, expect, afterAll, afterEach } from 'vitest';
import { CashuMint } from '../src/CashuMint.js';
import { CashuWallet } from '../src/CashuWallet.js';
import { HttpResponse, http } from 'msw';
import { setupServer } from 'msw/node';
import { setGlobalRequestOptions } from '../src/request.js';
import { MeltQuoteResponse } from '../src/model/types/index.js';

let request: Record<string, string> | undefined;
const mintUrl = 'https://localhost:3338';
const unit = 'sats';
const invoice =
'lnbc20u1p3u27nppp5pm074ffk6m42lvae8c6847z7xuvhyknwgkk7pzdce47grf2ksqwsdpv2phhwetjv4jzqcneypqyc6t8dp6xu6twva2xjuzzda6qcqzpgxqyz5vqsp5sw6n7cztudpl5m5jv3z6dtqpt2zhd3q6dwgftey9qxv09w82rgjq9qyyssqhtfl8wv7scwp5flqvmgjjh20nf6utvv5daw5h43h69yqfwjch7wnra3cn94qkscgewa33wvfh7guz76rzsfg9pwlk8mqd27wavf2udsq3yeuju';

const server = setupServer();

beforeAll(() => {
nock.disableNetConnect();
server.listen({ onUnhandledRequest: 'error' });
});

afterEach(() => {
server.resetHandlers();
});

beforeEach(() => {
nock.cleanAll();
request = undefined;
afterAll(() => {
server.close();
});

describe('requests', () => {
test('request with body contains the correct headers', async () => {
const mint = new CashuMint(mintUrl);
nock(mintUrl)
.get('/v1/melt/quote/bolt11/test')
.reply(200, function () {
request = this.req.headers;
console.log(this.req.headers);
return {
let headers: Headers;

server.use(
http.get(mintUrl + '/v1/melt/quote/bolt11/test', ({ request }) => {
headers = request.headers;
return HttpResponse.json({
quote: 'test_melt_quote_id',
amount: 2000,
fee_reserve: 20,
payment_preimage: null,
state: 'UNPAID'
} as MeltQuoteResponse;
});

});
})
);
const wallet = new CashuWallet(mint, { unit });
await wallet.checkMeltQuote('test');

expect(request).toBeDefined();
expect(headers!).toBeDefined();
// expect(request!['content-type']).toContain('application/json');
expect(request!['accept']).toContain('application/json, text/plain, */*');
expect(headers!.get('accept')).toContain('application/json, text/plain, */*');
});
test('global custom headers can be set', async () => {
let headers: Headers;
const mint = new CashuMint(mintUrl);
nock(mintUrl)
.get('/v1/melt/quote/bolt11/test')
.reply(200, function () {
request = this.req.headers;
return {
server.use(
http.get(mintUrl + '/v1/melt/quote/bolt11/test', ({ request }) => {
headers = request.headers;
return HttpResponse.json({
quote: 'test_melt_quote_id',
amount: 2000,
fee_reserve: 20,
payment_preimage: null,
state: 'UNPAID'
} as MeltQuoteResponse;
});
});
})
);

const wallet = new CashuWallet(mint, { unit });
setGlobalRequestOptions({ headers: { 'x-cashu': 'xyz-123-abc' } });

await wallet.checkMeltQuote('test');

expect(request).toBeDefined();
expect(request!['x-cashu']).toContain('xyz-123-abc');
expect(headers!).toBeDefined();
expect(headers!.get('x-cashu')).toContain('xyz-123-abc');
});
});

0 comments on commit ba23325

Please sign in to comment.