Skip to content
This repository has been archived by the owner on Sep 18, 2024. It is now read-only.

Commit

Permalink
updated tests to match changes in code
Browse files Browse the repository at this point in the history
  • Loading branch information
poef committed Dec 14, 2023
1 parent ba77088 commit 1a5a0ff
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 7 deletions.
28 changes: 28 additions & 0 deletions test/client.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import tap from 'tap'
import * as metro from '../src/metro.mjs'
import echomw from '../src/mw/echo.mock.mjs'

tap.test('start', async t => {
let c = metro.client(
Expand All @@ -14,6 +15,24 @@ tap.test('start', async t => {
t.end()
})

tap.test('start', async t => {
let c = metro.client(
{
middlewares: (req,next) => {
if (req.url=='https://example.com/') {
return new Response('This is the body')
} else {
return new Response(req)
}
}
}
)
let res = await c.get('https://example.com/')
let content = await res.text()
t.equal(content, 'This is the body')
t.end()
})

tap.test('withFunction', async t => {
let c = metro.client()
c = c.with((req,next) => metro.response('This is the body'))
Expand All @@ -36,4 +55,13 @@ tap.test('tracers', async t => {
t.equal(''+trace[0].response.body, 'This is the body')
t.equal(''+res.body, 'This is the body')
t.end()
})

tap.test('govert', async t => {
let url = 'http://localhost:3000/query/'
let client = metro.client(url).with(echomw())
let response = await client.post({body:'foo'})
let body = await response.text()
t.equal(body, 'foo')
t.end()
})
13 changes: 12 additions & 1 deletion test/request.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,17 @@ tap.test('bodyObject', t => {
t.end()
})

tap.test('bodyString', t => {
let body = "bar"
let req = metro.request('https://example.com', {
method: 'POST',
body: body
})
t.ok(req.body instanceof ReadableStream)
t.equal(req.body+'', 'bar')
t.end()
})

tap.test('bodyReadableStream', async t => {
let req = metro.request('https://example.com', {
method: 'POST',
Expand All @@ -107,7 +118,7 @@ tap.test('bodyFunction', t => {
body: 'This is the body'
}, {
body: (b, r) => {
return b+' altered'
r.body = b+' altered'
}
})
t.equal(''+req.body, 'This is the body altered')
Expand Down
2 changes: 1 addition & 1 deletion test/response.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ tap.test('bodyReadableStream', async t => {
tap.test('bodyFunction', t => {
let res = metro.response('This is the body', {
body: (b, r) => {
return b+' altered'
r.body = b+' altered'
}
})
t.equal(''+res.body, 'This is the body altered')
Expand Down
7 changes: 2 additions & 5 deletions test/url.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,7 @@ tap.test('search4', t => {
tap.test('searchFunction', t => {
let url = metro.url('https://example.com?foo=bar', {
search: (search, url) => {
let sp = new URLSearchParams(search)
sp.delete('foo')
return sp
url.searchParams.delete('foo')
}
})
t.equal(url.href, 'https://example.com/')
Expand Down Expand Up @@ -105,9 +103,8 @@ tap.test('port', t => {
tap.test('username', t => {
let url = metro.url('https://example.com/?user=foo', {
username: (username, url) => {
username = url.searchParams.get('user')
url.username = url.searchParams.get('user')
url.searchParams.delete('user')
return username
}
})
t.equal(url.href, 'https://[email protected]/')
Expand Down

0 comments on commit 1a5a0ff

Please sign in to comment.