From f78ec62feee14ae0236e9a7582b6f444229c3a9d Mon Sep 17 00:00:00 2001 From: Dan Hough Date: Fri, 4 Dec 2015 16:07:54 +0700 Subject: [PATCH] Started media tests for #19 --- lib/fluent.js | 5 +++ lib/route.js | 5 +++ lib/server.js | 13 +++++-- tests/assets/10x10-imagejpg.jpg | Bin 0 -> 769 bytes tests/assets/10x10-imagepng.png | Bin 0 -> 100 bytes tests/assets/20x20-image-redjpg.jpg | Bin 0 -> 792 bytes tests/assets/20x20-image-redpng.png | Bin 0 -> 118 bytes tests/media.test.js | 57 ++++++++++++++++++++++++++++ 8 files changed, 76 insertions(+), 4 deletions(-) create mode 100644 tests/assets/10x10-imagejpg.jpg create mode 100644 tests/assets/10x10-imagepng.png create mode 100644 tests/assets/20x20-image-redjpg.jpg create mode 100644 tests/assets/20x20-image-redpng.png create mode 100644 tests/media.test.js diff --git a/lib/fluent.js b/lib/fluent.js index 4f61e7d..1dca406 100644 --- a/lib/fluent.js +++ b/lib/fluent.js @@ -99,6 +99,11 @@ function FluentInterface(server, o) { route.setResponseBody(body); return this; }, + image: function (image) { + debug('Setting image to', image); + route.setResponseImage(image); + return this; + }, echo: function (echo) { if (typeof echo === 'undefined') { echo = true; diff --git a/lib/route.js b/lib/route.js index e476469..606f5dc 100644 --- a/lib/route.js +++ b/lib/route.js @@ -96,6 +96,11 @@ Route.prototype.setResponseBody = function (body) { this.response.body = body; }; +Route.prototype.setResponseImage = function (image) { + this.debug('Now returning image called', image); + this.response.image = image; +}; + Route.prototype.setProxyURL = function (url) { this.debug('The route is now a proxy of', this.request.method, url); this.response.proxy = url; diff --git a/lib/server.js b/lib/server.js index 8e24f89..bd40a1b 100644 --- a/lib/server.js +++ b/lib/server.js @@ -78,10 +78,11 @@ function Interfake(o) { url: req.path, query: req.query }; - var rootPathRegex, responseBody, responseStatusCode, responseHeaders, expectedRoute, expectedRoutes; + var rootPathRegex, responseBody, responseImage, responseStatusCode, responseHeaders, expectedRoute, expectedRoutes; var proxyQueryStrings; var sendResponse = function () { var responseBodyLength = 0; + if (responseBody) { responseBodyLength = JSON.stringify(responseBody).length; responseBody = JSON.parse(JSON.stringify(responseBody)); @@ -101,7 +102,12 @@ function Interfake(o) { debug('Sending response now'); - res.status(responseStatusCode).send(responseBody); + if (responseImage) { + res.status(responseStatusCode).sendFile(path.resolve(process.cwd(), responseImage)); + } else { + res.setHeader('Content-Type', 'application/json'); + res.status(responseStatusCode).send(responseBody); + } }; if (o.path.length > 1) { @@ -166,6 +172,7 @@ function Interfake(o) { }); } else { responseBody = specifiedResponse.body; + responseImage = specifiedResponse.image; responseStatusCode = specifiedResponse.code; responseHeaders = specifiedResponse.headers; @@ -180,8 +187,6 @@ function Interfake(o) { debug(req.method, 'request to', req.url, 'will be delayed by', responseDelay, 'millis'); debug('Expected route is', expectedRoute); - res.setHeader('Content-Type', 'application/json'); - setTimeout(function() { var modification; debug('Expected response is', specifiedResponse); diff --git a/tests/assets/10x10-imagejpg.jpg b/tests/assets/10x10-imagejpg.jpg new file mode 100644 index 0000000000000000000000000000000000000000..333077865864d89b394642cc40a86d0c63c88a79 GIT binary patch literal 769 zcmex=~Yw&!L) z!zPxO4V*LH{BWxKe}F-dgMo{Ii8LvnOK-vSwYq?)&k`jSOi&x6b&8OgaZ@Vl?p|S8YeE~Pwh=DOELf4NWZ*Q!{f5ODks=S2uSLPp{yR(6I1`$f)F$)U@=B%&g*) z(z5c3%Btp;*0%PJ&aO$5r%atTea6gLixw|gx@`H1m8&*w-m-Pu_8mKS9XfpE=&|D` zPM*4S`O4L6*Kgds_3+W-Cr_U}fAR9w$4{TXeEs(Q$Io9Ne=#yJL%ap|8JfR3M&~ka)>xhT)6Qdr?PR-2hpUWi(FzVCJ$9V zg1iRy8F3zKBFkrRk0JbZo52~Bm?#4Ofre0I07&!yyTD}0^#2wE4>Kb$F)<4=*fae5 Or@^p*|NjLjeG>pmN85q` literal 0 HcmV?d00001 diff --git a/tests/assets/10x10-imagepng.png b/tests/assets/10x10-imagepng.png new file mode 100644 index 0000000000000000000000000000000000000000..4c7ee04f22992025420c77f26e3dd9718faca6a8 GIT binary patch literal 100 zcmeAS@N?(olHy`uVBq!ia0vp^AT}2V8<6ZZI=>f4v7|ftIx;Y9?C1WI$O`03d%8G= wNJu9C;b-A-VNg@L(lE70>xgua3p+nU<}4P0D`F8JfXWy=UHx3vIVCg!0NZ{S4*&oF literal 0 HcmV?d00001 diff --git a/tests/assets/20x20-image-redjpg.jpg b/tests/assets/20x20-image-redjpg.jpg new file mode 100644 index 0000000000000000000000000000000000000000..af82214631d33437f3383a10343da3e6ccd4fedf GIT binary patch literal 792 zcmex=~Yw&!L) z!zPxO4V*LH{BWxKe}F-dgF%Ergqcy0fk}{&S&;Gn5e9jn&8$H8ft&~hj7-cdtZeKY zoLt;M1zQCem>8LvnOK-vSwYq?)&k`jSOi&x6b&8OgaZ@Vl?p|S8YeE~Pwh=DOELf4NWZ*Q!{f5ODks=S2uSLPp{yR(6I1`$f)F$)U@=B%&g*) z(z5c3%Btp;*0%PJ&aO$5r%atTea6gLixw|gx@`H1m8&*w-m-Pu_8mKS9XfpE=&|D` zPM*4S`O4L6*Kgds_3+W-Cr_U}fAR9w$4{TXeEs(Q$Io9Ne=#yJL%ap|8JfR3M&~ka)>xhT)6Qdr?PR-2hpUWi(FzVCJ$9V zg1iRy8F3zKBFkrRk0JbZo52~Bm?#4Ofre0I07&!yyTD}0{QnjM4>Kb$F)<4=*fad$ XU;pIuzf(*NTnzs&AagDvb8Z3vA+za( literal 0 HcmV?d00001 diff --git a/tests/assets/20x20-image-redpng.png b/tests/assets/20x20-image-redpng.png new file mode 100644 index 0000000000000000000000000000000000000000..38b90338aaa895dae3a6cf387271003e736a62cd GIT binary patch literal 118 zcmeAS@N?(olHy`uVBq!ia0vp^A|TAc1|)ksWqE-VOS+@4BLl<6e(pbstU$i5r;B5V zg<$fY11f*#H+D62cBM7;N|{dhBp}Os_@aS|GK