11'use strict' ;
22
3- let FastBoot = require ( 'fastboot' ) ;
4- let url = require ( 'url' ) ;
53let resolve = require ( 'resolve' ) ;
6- let nock = require ( 'nock' ) ;
7- let bodyParser = require ( 'body-parser' ) ;
8- let path = require ( 'path' ) ;
9- let fs = require ( 'fs' ) ;
104let minimist = require ( 'minimist' ) ;
11- let JSONfn = require ( 'json-fn' ) ;
5+
6+ let {
7+ createCleanUpMocks,
8+ createFastbootEcho,
9+ createFastbootTest,
10+ createMockRequest,
11+ reloadServer,
12+ createServer,
13+ makeFastbootTestingConfig
14+ } = require ( './lib/helpers' ) ;
1215
1316module . exports = {
1417 name : 'ember-cli-fastboot-testing' ,
@@ -46,95 +49,31 @@ module.exports = {
4649
4750 postBuild ( result ) {
4851 let distPath = result . directory ;
49- let options = this . makeFastbootTestingConfig ( { distPath } ) ;
52+ let { pkg } = this . project ;
5053
5154 if ( this . fastboot ) {
52- this . fastboot . reload ( { distPath } ) ;
53- options . setupFastboot ( this . fastboot ) ;
55+ let options = makeFastbootTestingConfig ( { distPath } , pkg ) ;
56+ reloadServer ( this . fastboot , distPath , options ) ;
5457 } else {
55- this . _createServer ( distPath )
58+ this . fastboot = createServer ( distPath , pkg ) ;
5659 }
5760
5861 return result ;
5962 } ,
6063
61- makeFastbootTestingConfig ( config ) {
62- let defaults = {
63- setupFastboot ( ) { }
64- } ;
65-
66- let configPath = 'config' ;
67- let pkg = this . project . pkg ;
68-
69- if ( pkg [ 'ember-addon' ] && pkg [ 'ember-addon' ] [ 'configPath' ] ) {
70- configPath = pkg [ 'ember-addon' ] [ 'configPath' ] ;
71- }
72-
73- let fastbootTestConfigPath = path . resolve ( configPath , 'fastboot-testing.js' ) ;
74-
75- let customized = fs . existsSync ( fastbootTestConfigPath ) ?
76- require ( fastbootTestConfigPath ) :
77- { } ;
78-
79- return Object . assign ( { } , config , defaults , customized ) ;
80- } ,
81-
82- _createServer ( distPath ) {
83- let options = this . makeFastbootTestingConfig ( { distPath } ) ;
84- this . fastboot = new FastBoot ( options ) ;
85- options . setupFastboot ( this . fastboot ) ;
86- } ,
87-
8864 _fastbootRenderingMiddleware ( app ) {
89- app . post ( '/__mock-request' , bodyParser . json ( { limit : '50mb' } ) , ( req , res ) => {
90- let mock = nock ( req . headers . origin )
91- . persist ( )
92- . intercept ( req . body . path , req . body . method )
93- . reply ( req . body . statusCode , req . body . response ) ;
94-
95- res . json ( { mocks : mock . pendingMocks ( ) } ) ;
96- } ) ;
97-
98- app . use ( '/__cleanup-mocks' , ( req , res ) => {
99- nock . cleanAll ( )
100-
101- res . json ( { ok : true } ) ;
102- } ) ;
103-
104- app . post ( '/__fastboot-testing' , bodyParser . json ( ) , ( req , res ) => {
105- let urlToVisit = decodeURIComponent ( req . body . url ) ;
106- let parsed = url . parse ( urlToVisit , true ) ;
107-
108- let headers = Object . assign (
109- { } ,
110- req . headers ,
111- JSONfn . parse ( req . body . options ) . headers || { }
112- ) ;
113-
114- let defaultOptions = {
115- request : {
116- method : 'GET' ,
117- protocol : 'http' ,
118- url : parsed . path ,
119- query : parsed . query ,
120- headers,
121- } ,
122- response : { } ,
123- } ;
124-
125- let options = Object . assign ( defaultOptions , JSONfn . parse ( req . body . options ) ) ;
126-
127- res . set ( 'x-fastboot-testing' , true ) ;
128-
65+ createMockRequest ( app ) ;
66+ createCleanUpMocks ( app ) ;
67+ createFastbootTest ( app , ( { res, options, urlToVisit} ) => {
12968 if ( ! this . fastboot ) {
13069 const path = minimist ( process . argv . slice ( 2 ) ) . path ;
13170 if ( path ) {
132- this . _createServer ( path ) ;
71+ this . fastboot = createServer ( path , this . project . pkg ) ;
13372 } else {
13473 return res . json ( { err : 'no path found' } ) ;
13574 }
13675 }
137-
76+
13877 this . fastboot
13978 . visit ( urlToVisit , options )
14079 . then ( page => {
@@ -151,11 +90,11 @@ module.exports = {
15190 . catch ( err => {
15291 let errorObject ;
15392 let jsonError = { } ;
154-
93+
15594 errorObject = ( typeof err === 'string' ) ?
15695 new Error ( err ) :
15796 err ;
158-
97+
15998 // we need to copy these properties off the error
16099 // object into a pojo that can be serialized and
161100 // sent over the wire to the test runner.
@@ -168,18 +107,16 @@ module.exports = {
168107 'number' ,
169108 'stack'
170109 ] ;
171-
110+
172111 errorProps . forEach ( key => jsonError [ key ] = errorObject [ key ] ) ;
173-
112+
174113 res . json ( { err : jsonError } ) ;
175114 } ) ;
176115 } ) ;
177116
178117 if ( this . app && this . app . name === "dummy" ) {
179118 // our dummy app has an echo endpoint!
180- app . post ( '/fastboot-testing/echo' , bodyParser . text ( ) , ( req , res ) => {
181- res . send ( req . body ) ;
182- } ) ;
119+ createFastbootEcho ( app ) ;
183120 }
184121 } ,
185122} ;
0 commit comments