Skip to content

Commit 206587b

Browse files
committed
share page test
1 parent 5107ad4 commit 206587b

File tree

6 files changed

+83
-17
lines changed

6 files changed

+83
-17
lines changed

lib/miniprofiler.js

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -89,32 +89,53 @@ function results(req, res) {
8989
var proc = function(post) {
9090
// todo: store client timings
9191
var id = post.id || url.parse(req.url, true).query.id;
92-
var s = storage(id);
93-
if (post.popup == '1')
94-
res.end(s);
95-
else {
96-
var j = JSON.parse(s);
92+
var data = storage(id);
93+
if (!data) {
94+
debug(`Id '${id}' not found.`);
9795
res.setHeader('Content-Type', 'text/html');
98-
res.end(includes.share({
99-
name: j.Name,
100-
duration: j.DurationMilliseconds,
101-
path: resourcePath,
102-
json: s,
103-
includes: include(id),
104-
version: version
105-
}));
106-
}
96+
res.writeHead(404);
97+
res.end(`Id '${id}' not found.`);
98+
return;
99+
}
100+
101+
if (post.popup == '1') {
102+
res.setHeader('Content-Type', 'application/json');
103+
res.end(data);
104+
return;
105+
}
106+
107+
var json = JSON.parse(data);
108+
res.setHeader('Content-Type', 'text/html');
109+
res.end(includes.share({
110+
name: json.Name,
111+
duration: json.DurationMilliseconds,
112+
path: resourcePath,
113+
json: data,
114+
includes: include(id),
115+
version: version
116+
}));
117+
107118
};
119+
120+
/*
121+
122+
https://nodejs.org/api/http.html#http_class_http_clientrequest
123+
Request does not have `body` attribute
124+
Do we still need this?
125+
108126
if(req.body) {
109127
proc(req.body);
110128
return;
111129
}
130+
*/
131+
112132
var body = '';
113133
req.on('data', function(data) {
114134
body += data;
115135
if (body.length > 1e6)
116136
req.connection.destroy();
117137
});
138+
118139
req.on('end', function() {
119140
var post = qs.parse(body);
120141
proc(post);

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
"main": "lib/miniprofiler.js",
66
"scripts": {
77
"lint": "eslint .",
8-
"test": "istanbul cover node_modules/mocha/bin/_mocha tests/"
8+
"test": "istanbul cover node_modules/mocha/bin/_mocha tests/",
9+
"example": "node examples/express/server.js"
910
},
1011
"repository": {
1112
"type": "git",
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ var expect = require('chai').expect;
22
var server = require('./server');
33
var fs = require('fs');
44

5-
describe('MiniProfiler Static Assets Tests', function() {
5+
describe('MiniProfiler Assets Tests', function() {
66
before(server.start);
77
after(server.stop);
88

tests/basic-test.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,13 @@ describe('MiniProfiler Tests', function() {
55
before(server.start);
66
after(server.stop);
77

8+
it('Profiled routes should always return Profiler ID', function(done) {
9+
server.get('/', (err, response) => {
10+
expect(response.headers).to.include.keys('x-miniprofiler-ids');
11+
done();
12+
});
13+
});
14+
815
it('Unprofiled route should not return Profiler ID', function(done) {
916
server.get('/unprofiled', (err, response) => {
1017
expect(response.headers).to.not.include.keys('x-miniprofiler-ids');

tests/server/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
var miniprofiler = require('../lib/miniprofiler.js');
1+
var miniprofiler = require('../../lib/miniprofiler.js');
22
var express = require('express');
33
var request = require('request');
44

tests/share-test.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
var expect = require('chai').expect;
2+
var server = require('./server');
3+
4+
describe('MiniProfiler Share Tests', function() {
5+
before(server.start);
6+
after(server.stop);
7+
8+
it('Valid profiled id should render share page', function(done) {
9+
server.get('/', (err, response) => {
10+
var ids = JSON.parse(response.headers['x-miniprofiler-ids']);
11+
12+
server.get(`/mini-profiler-resources/results?id=${ids[0]}`, (err, response, body) => {
13+
expect(response.statusCode).to.be.equal(200);
14+
expect(response.headers['content-type']).to.be.equal('text/html');
15+
done();
16+
});
17+
18+
});
19+
});
20+
21+
it('Invalid profiled id should render 404', function(done) {
22+
server.get(`/mini-profiler-resources/results?id=123`, (err, response, body) => {
23+
expect(response.statusCode).to.be.equal(404);
24+
expect(response.headers['content-type']).to.be.equal('text/html');
25+
done();
26+
});
27+
});
28+
29+
it('Invalid profiled id should render 404', function(done) {
30+
server.post(`/mini-profiler-resources/results`, { id: 123 }, (err, response, body) => {
31+
expect(response.statusCode).to.be.equal(404);
32+
expect(response.headers['content-type']).to.be.equal('text/html');
33+
done();
34+
});
35+
});
36+
37+
});

0 commit comments

Comments
 (0)