Skip to content

Commit 704e5fa

Browse files
committed
Feat: Adding a test server
1 parent b5ad369 commit 704e5fa

File tree

8 files changed

+47
-28
lines changed

8 files changed

+47
-28
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
"build-dts": "tsc && prettier --write ./publish/src",
2323
"build-strict": "pnpm test-dev && pnpm build && pnpm test-pro",
2424
"start": "rollup --config script/start.mjs",
25+
"start-server": "rollup --config script/server.mjs",
2526
"test-dev": "jest test/environment/test.ts dev",
2627
"test-pro": "jest test/environment/test.ts pro",
2728
"test-crawlPage": "jest test/environment/crawlPage.test.ts dev",

script/server.mjs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import tsPlugin from 'rollup-plugin-typescript2'
2+
import terserPlugin from '@rollup/plugin-terser'
3+
import runPlugin from '@rollup/plugin-run'
4+
5+
export default {
6+
input: 'test/server/index.ts',
7+
output: {
8+
file: 'test/server/index.js',
9+
format: 'cjs'
10+
},
11+
plugins: [tsPlugin(), terserPlugin(), runPlugin({ stdin: { clear: true } })]
12+
}

script/start.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@ export default {
88
file: 'test/start/index.js',
99
format: 'cjs'
1010
},
11-
plugins: [tsPlugin(), terserPlugin(), runPlugin({ stdin: { clear: true } })]
11+
plugins: [tsPlugin(), runPlugin({ stdin: { clear: true } })]
1212
}

test/environment/crawlData.test.ts

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,7 @@ jest.setTimeout(60000)
2121
async function writtenString() {
2222
const testXCrawl = xCrawl()
2323

24-
const res = await testXCrawl.crawlData(
25-
'http://localhost:9001/api/room/193581217'
26-
)
24+
const res = await testXCrawl.crawlData('http://localhost:8888')
2725

2826
return res.isSuccess
2927
}
@@ -33,7 +31,7 @@ async function writtenCrawlDataDetailConfig() {
3331
const testXCrawl = xCrawl()
3432

3533
const res = await testXCrawl.crawlData({
36-
url: 'http://localhost:9001/api/room/193581217'
34+
url: 'http://localhost:8888'
3735
})
3836

3937
return res.isSuccess
@@ -44,8 +42,8 @@ async function writtenStringAndCrawlDataDetailConfigArr() {
4442
const testXCrawl = xCrawl()
4543

4644
const res = await testXCrawl.crawlData([
47-
'http://localhost:9001/api/room/193581217',
48-
{ url: 'http://localhost:9001/api/room/193581217' }
45+
'http://localhost:8888',
46+
{ url: 'http://localhost:8888' }
4947
])
5048

5149
return res.reduce((prev, item) => prev && item.isSuccess, true)
@@ -56,10 +54,7 @@ async function writtenCrawlDataAdvancedConfig() {
5654
const testXCrawl = xCrawl()
5755

5856
const res = await testXCrawl.crawlData({
59-
targets: [
60-
'http://localhost:9001/api/room/193581217',
61-
{ url: 'http://localhost:9001/api/room/193581217' }
62-
]
57+
targets: ['http://localhost:8888', { url: 'http://localhost:8888' }]
6358
})
6459

6560
return res.reduce((prev, item) => prev && item.isSuccess, true)
@@ -69,27 +64,27 @@ async function writtenCrawlDataAdvancedConfig() {
6964
// 2.1.Loader Base Config
7065
async function loaderBaseConfig() {
7166
const testXCrawl = xCrawl({
72-
baseUrl: 'http://localhost:9001/api',
73-
proxy: 'http://localhost:14892',
67+
baseUrl: 'http://localhost:8888',
68+
proxy: { urls: ['http://localhost:14892'] },
7469
timeout: 10000,
7570
intervalTime: { max: 1000 },
7671
maxRetry: 0
7772
})
7873

79-
const res = await testXCrawl.crawlData(['/room/193581217', '/room/193581217'])
74+
const res = await testXCrawl.crawlData(['/', '/'])
8075

8176
return res.reduce((prev, item) => prev && item.isSuccess, true)
8277
}
8378

8479
// 2.2.Loader Advanced Config
8580
async function loaderAdvancedConfig() {
8681
const testXCrawl = xCrawl({
87-
baseUrl: 'http://localhost:9001/api'
82+
baseUrl: 'http://localhost:8888'
8883
})
8984

9085
const res = await testXCrawl.crawlData({
91-
targets: ['/room/193581217', '/room/193581217'],
92-
proxy: 'http://localhost:14892',
86+
targets: ['/', '/'],
87+
proxy: { urls: ['http://localhost:14892'] },
9388
timeout: 10000,
9489
intervalTime: { max: 1000 },
9590
maxRetry: 0

test/environment/crawlFile.test.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ const storeDir = path.resolve(__dirname, './upload')
2727
/* 1.Written */
2828
// 1.1.written CrawlFileDetailConfig
2929
async function writtenCrawlFileDetailConfig() {
30-
const testXCrawl = xCrawl({ proxy: 'http://localhost:14892' })
30+
const testXCrawl = xCrawl({ proxy: { urls: ['http://localhost:14892'] } })
3131

3232
const res = await testXCrawl.crawlFile({ url: urls[0], storeDir })
3333

@@ -36,7 +36,7 @@ async function writtenCrawlFileDetailConfig() {
3636

3737
// 1.2.written CrawlFileDetailConfig[]
3838
async function writtenCrawlFileDetailConfigArr() {
39-
const testXCrawl = xCrawl({ proxy: 'http://localhost:14892' })
39+
const testXCrawl = xCrawl({ proxy: { urls: ['http://localhost:14892'] } })
4040

4141
const res = await testXCrawl.crawlFile(urls.map((url) => ({ url, storeDir })))
4242

@@ -48,7 +48,7 @@ async function writtenCrawlFileDetailConfigArr() {
4848

4949
// 1.3.written CrawlFileAdvancedConfig
5050
async function writtenCrawlFileAdvancedConfig() {
51-
const testXCrawl = xCrawl({ proxy: 'http://localhost:14892' })
51+
const testXCrawl = xCrawl({ proxy: { urls: ['http://localhost:14892'] } })
5252

5353
const res = await testXCrawl.crawlFile({
5454
targets: urls,
@@ -67,7 +67,7 @@ async function loaderBaseConfig() {
6767
const testXCrawl = xCrawl({
6868
baseUrl:
6969
'https://raw.githubusercontent.com/coder-hxl/airbnb-upload/master/area',
70-
proxy: 'http://localhost:14892',
70+
proxy: { urls: ['http://localhost:14892'] },
7171
timeout: 10000,
7272
intervalTime: { max: 1000 },
7373
maxRetry: 0
@@ -90,7 +90,7 @@ async function loaderAdvancedConfig() {
9090

9191
const res = await testXCrawl.crawlFile({
9292
targets: ['/4401.jpg', '/4403.jpg'],
93-
proxy: 'http://localhost:14892',
93+
proxy: { urls: ['http://localhost:14892'] },
9494
timeout: 10000,
9595
storeDir,
9696
intervalTime: { max: 1000 },
@@ -105,7 +105,7 @@ async function storeConfig() {
105105
const testXCrawl = xCrawl({
106106
baseUrl:
107107
'https://raw.githubusercontent.com/coder-hxl/airbnb-upload/master/area',
108-
proxy: 'http://localhost:14892'
108+
proxy: { urls: ['http://localhost:14892'] }
109109
})
110110

111111
const record: string[] = []

test/environment/crawlPage.test.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ async function writtenString() {
3030

3131
// 1.2.written CrawlPageDetailConfig
3232
async function writtenCrawlPageDetailConfig() {
33-
const testXCrawl = xCrawl({ proxy: 'http://localohst:14892' })
33+
const testXCrawl = xCrawl({ proxy: { urls: ['http://localhost:14892'] } })
3434

3535
const res = await testXCrawl.crawlPage({
3636
url: 'https://github.com/coder-hxl/x-crawl'
@@ -43,7 +43,7 @@ async function writtenCrawlPageDetailConfig() {
4343

4444
// 1.3.written (string | CrawlPageDetailConfig)[]
4545
async function writtenStringAndCrawlPageDetailConfigArr() {
46-
const testXCrawl = xCrawl({ proxy: 'http://localohst:14892' })
46+
const testXCrawl = xCrawl({ proxy: { urls: ['http://localhost:14892'] } })
4747

4848
const res = await testXCrawl.crawlPage([
4949
'https://github.com/coder-hxl/x-crawl',
@@ -57,7 +57,7 @@ async function writtenStringAndCrawlPageDetailConfigArr() {
5757

5858
// 1.4.written CrawlPageAdvancedConfig
5959
async function writtenCrawlPageAdvancedConfig() {
60-
const testXCrawl = xCrawl({ proxy: 'http://localohst:14892' })
60+
const testXCrawl = xCrawl({ proxy: { urls: ['http://localhost:14892'] } })
6161

6262
const res = await testXCrawl.crawlPage({
6363
targets: [
@@ -76,7 +76,7 @@ async function writtenCrawlPageAdvancedConfig() {
7676
async function loaderBaseConfig() {
7777
const testXCrawl = xCrawl({
7878
baseUrl: 'https://github.com',
79-
proxy: 'http://localhost:14892',
79+
proxy: { urls: ['http://localhost:14892'] },
8080
timeout: 10000,
8181
intervalTime: { max: 1000 },
8282
maxRetry: 0
@@ -95,7 +95,7 @@ async function loaderAdvancedConfig() {
9595

9696
const res = await testXCrawl.crawlPage({
9797
targets: ['/coder-hxl', '/coder-hxl/x-crawl'],
98-
proxy: 'http://localhost:14892',
98+
proxy: { urls: ['http://localhost:14892'] },
9999
timeout: 10000,
100100
intervalTime: { max: 1000 },
101101
maxRetry: 0

test/server/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
"use strict";require("node:http").createServer(((e,t)=>{t.setHeader("Content-type","text/plain"),t.end("success")})).listen(8888,(()=>{console.log("服务器在 8888 端口启动成功~")}));

test/server/index.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import http from 'node:http'
2+
3+
http
4+
.createServer((req, res) => {
5+
res.setHeader('Content-type', 'text/plain')
6+
res.end('success')
7+
})
8+
.listen(8888, () => {
9+
console.log(`服务器在 8888 端口启动成功~`)
10+
})

0 commit comments

Comments
 (0)