-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
148 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
|
||
*.log | ||
yarn.lock | ||
|
||
node_modules/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
language: node_js | ||
node_js: | ||
- "stable" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
#!/usr/bin/env node | ||
|
||
const xttp = require('..'); | ||
const http = require('http'); | ||
|
||
const [method, url] = process.argv.slice(2); | ||
|
||
function color(str, c){ | ||
return "\x1b[" + c + "m" + str + "\x1b[0m"; | ||
}; | ||
|
||
const header = res => { | ||
const { httpVersion } = res.response; | ||
const { status, statusText } = res; | ||
console.log(color(`HTTP/${httpVersion} ${status} ${statusText}`, 32)); | ||
Object.keys(res.headers).forEach(header => { | ||
console.log(`${color(header, 36)}: ${res.headers[header]}`); | ||
}); | ||
return res.auto(); | ||
}; | ||
|
||
const response = body => { | ||
console.log(); | ||
console.log(body); | ||
}; | ||
|
||
xttp({ | ||
method, | ||
url | ||
}) | ||
.then(header) | ||
.then(response) | ||
.catch(err => console.error(err)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1 @@ | ||
const xttp = require('..'); | ||
|
||
// xttp | ||
// .get('https://httpbin.org/get?a=b') | ||
// .then(res => res.json()) | ||
// .then(res => console.log(res)); | ||
|
||
// // case 1: | ||
xttp('https://httpbin.org/post', { | ||
method: 'post', | ||
headers: { | ||
'Content-Type': 'application/json; charset=utf-8' | ||
}, | ||
body: '{}' | ||
}) | ||
.then(res => res.json()) | ||
.then(res => { | ||
console.log(res); | ||
}); | ||
|
||
// // case 2: | ||
// xttp('https://httpbin.org/get?a=b', { | ||
// method: 'get', | ||
// query: { c: 'd' }, | ||
// headers: { | ||
// 'User-Agent': 'xttp/1.0' | ||
// } | ||
// }) | ||
// .then(res => res.json()) | ||
// .then(res => { | ||
// console.log(res); | ||
// }); | ||
|
||
// case 3: | ||
// xttp | ||
// .create({ | ||
// url: 'aaa', | ||
// query: { a: 'b' } | ||
// }) | ||
// .header('') | ||
// .post('https://httpbin.org/post?e=f') | ||
// .query('c', 'd') | ||
// .query({ g: 'h' }) | ||
// .json({ name: 'xttp' }) | ||
// .then(res => res.json()) | ||
// .then(res => { | ||
// console.log(res); | ||
// }); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
const xttp = require('..'); | ||
const test = require('./test'); | ||
const assert = require('assert'); | ||
|
||
test('xttp#request', async () => { | ||
const res = await xttp('https://httpbin.org/get?a=b'); | ||
assert.equal(res.status, 200); | ||
const body = await res.json(); | ||
assert.deepEqual(body.args, { a: 'b' }); | ||
assert.equal(res.contentType, 'application/json'); | ||
}); | ||
|
||
test('xttp#get', async () => { | ||
const res = await xttp.get('https://httpbin.org/get?a=b'); | ||
assert.equal(res.status, 200); | ||
}); | ||
|
||
test('xttp#post', async () => { | ||
const res = await xttp('https://httpbin.org/post', { | ||
method: 'post', | ||
headers: { | ||
'Content-Type': 'application/json; charset=utf-8' | ||
}, | ||
body: '{}' | ||
}); | ||
assert.equal(res.status, 200); | ||
}); | ||
|
||
test('xttp#get with query', async () => { | ||
const res = await xttp('https://httpbin.org/get?a=b', { | ||
method: 'get', | ||
query: { c: 'd' }, | ||
headers: { | ||
'User-Agent': 'xttp/1.0' | ||
} | ||
}); | ||
const body = await res.json(); | ||
assert.equal(res.status, 200); | ||
assert.deepEqual(body.args, { a: 'b', c: 'd' }); | ||
}); | ||
|
||
test('xttp#create', async () => { | ||
const res = await xttp | ||
.create({ | ||
url: 'aaa', | ||
query: { a: 'b' } | ||
}) | ||
.header('foo', 'bar') | ||
.post('https://httpbin.org/post?e=f') | ||
.query('c', 'd') | ||
.query({ g: 'h' }) | ||
.json({ name: 'xttp' }); | ||
const body = await res.json(); | ||
assert.equal(res.status, 200); | ||
assert.deepEqual(body.args, { a: 'b', c: 'd', e: 'f', g: 'h' }); | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
const { inspect } = require('util'); | ||
/** | ||
* super tiny testing framework | ||
* | ||
* @author Liu song <[email protected]> | ||
* @github https://github.com/song940 | ||
*/ | ||
const test = async (title, fn) => { | ||
try { | ||
await fn(); | ||
console.log(color(` ✔ ${title}`, 32)); | ||
} catch (err) { | ||
console.error(color(` ✘ ${title}`, 31)); | ||
console.log(); | ||
console.log(color(` ${err.name}`, 31)); | ||
console.error(color(` expected: ${inspect(err.expected)}`, 32)); | ||
console.error(color(` actual: ${inspect(err.actual)}`, 31)); | ||
console.log(); | ||
} | ||
}; | ||
|
||
function color(str, c) { | ||
return "\x1b[" + c + "m" + str + "\x1b[0m"; | ||
}; | ||
|
||
module.exports = test; |