Skip to content
This repository was archived by the owner on Nov 8, 2024. It is now read-only.

Commit 53b55c8

Browse files
committed
feat: simplify creating server files
Close #54
1 parent f11eed4 commit 53b55c8

File tree

5 files changed

+25
-62
lines changed

5 files changed

+25
-62
lines changed

features/execution_order.feature

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,7 @@ Feature: Execution order
22

33
Background:
44
Given I have Dredd installed
5-
And a file named "server.js" with:
6-
"""
7-
require('http')
8-
.createServer((req, res) => {
9-
if (req.url === '/message') {
10-
res.writeHead(200, { 'Content-Type': 'text/plain' });
11-
res.end('Hello World!\n');
12-
} else {
13-
res.writeHead(500);
14-
res.end();
15-
}
16-
})
17-
.listen(4567);
18-
"""
19-
5+
And a file "server.js" with a server responding on "http://localhost:4567/message" with "Hello World!"
206
And a file named "apiary.apib" with:
217
"""
228
# My Api

features/failing_transaction.feature

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,7 @@ Feature: Failing a transaction
22

33
Background:
44
Given I have Dredd installed
5-
And a file named "server.js" with:
6-
"""
7-
require('http')
8-
.createServer((req, res) => {
9-
if (req.url === '/message') {
10-
res.writeHead(200, { 'Content-Type': 'text/plain' });
11-
res.end('Hello World!\n');
12-
} else {
13-
res.writeHead(500);
14-
res.end();
15-
}
16-
})
17-
.listen(4567);
18-
"""
19-
5+
And a file "server.js" with a server responding on "http://localhost:4567/message" with "Hello World!"
206
And a file named "apiary.apib" with:
217
"""
228
# My Api

features/hook_handlers.feature

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,7 @@ Feature: Hook handlers
22

33
Background:
44
Given I have Dredd installed
5-
And a file named "server.js" with:
6-
"""
7-
require('http')
8-
.createServer((req, res) => {
9-
if (req.url === '/message') {
10-
res.writeHead(200, { 'Content-Type': 'text/plain' });
11-
res.end('Hello World!\n');
12-
} else {
13-
res.writeHead(500);
14-
res.end();
15-
}
16-
})
17-
.listen(4567);
18-
"""
19-
5+
And a file "server.js" with a server responding on "http://localhost:4567/message" with "Hello World!"
206
And a file named "apiary.apib" with:
217
"""
228
# My Api

features/multiple_hookfiles.feature

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,7 @@ Feature: Multiple hook files with a glob
22

33
Background:
44
Given I have Dredd installed
5-
And a file named "server.js" with:
6-
"""
7-
require('http')
8-
.createServer((req, res) => {
9-
if (req.url === '/message') {
10-
res.writeHead(200, { 'Content-Type': 'text/plain' });
11-
res.end('Hello World!\n');
12-
} else {
13-
res.writeHead(500);
14-
res.end();
15-
}
16-
})
17-
.listen(4567);
18-
"""
19-
5+
And a file "server.js" with a server responding on "http://localhost:4567/message" with "Hello World!"
206
And a file named "apiary.apib" with:
217
"""
228
# My Api

features/support/steps.js

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const childProcess = require('child_process');
55
const { expect } = require('chai');
66
const fs = require('fs-extra');
77
const net = require('net');
8+
const url = require('url');
89
const which = require('which');
910
const kill = require('tree-kill');
1011
const {
@@ -33,11 +34,29 @@ Given('I have Dredd installed', function step() {
3334
which.sync(this.dreddBin); // throws if not found
3435
});
3536

36-
Given(/^a file named "([^"]+)" with:$/, function step(filename, content) {
37+
Given('a file {string} with a server responding on {string} with {string}', function step(filename, fullURL, body) {
38+
const urlParts = url.parse(fullURL);
39+
const content = `
40+
require('http')
41+
.createServer((req, res) => {
42+
if (req.url === '${urlParts.path}') {
43+
res.writeHead(200, { 'Content-Type': 'text/plain' });
44+
res.end('${body}\\n');
45+
} else {
46+
res.writeHead(500);
47+
res.end();
48+
}
49+
})
50+
.listen(${urlParts.port});
51+
`;
52+
fs.writeFileSync(path.join(this.dir, filename), content);
53+
});
54+
55+
Given('a file named {string} with:', function step(filename, content) {
3756
fs.writeFileSync(path.join(this.dir, filename), content);
3857
});
3958

40-
Given(/^I set the environment variables to:$/, function step(env) {
59+
Given('I set the environment variables to:', function step(env) {
4160
this.env = { ...this.env, ...env.rowsHash() };
4261
});
4362

0 commit comments

Comments
 (0)