@@ -2,22 +2,37 @@ var spawn = require('child_process').spawn;
22var path = require ( 'path' ) . posix ;
33var url = require ( 'url' ) ;
44var backend = require ( 'git-http-backend' ) ;
5+ var fixturez = require ( 'fixturez' ) ;
56
6- function getGitDir ( req ) {
7- var u = url . parse ( req . url )
8- if ( req . method === 'GET' && u . pathname . endsWith ( '/info/refs' ) ) {
9- return u . pathname . replace ( / \/ i n f o \/ r e f s $ / , '' ) . replace ( / ^ \/ / , '' )
10- }
11- if ( req . method === 'POST' && req . headers [ 'content-type' ] === 'application/x-git-upload-pack-request' ) {
12- return u . pathname . replace ( / \/ g i t - u p l o a d - p a c k $ / , '' ) . replace ( / ^ \/ / , '' )
13- }
14- if ( req . method === 'POST' && req . headers [ 'content-type' ] === 'application/x-git-receive-pack-request' ) {
15- return u . pathname . replace ( / \/ g i t - r e c e i v e - p a c k $ / , '' ) . replace ( / ^ \/ / , '' )
7+ function factory ( config ) {
8+ if ( ! config . root ) throw new Error ( 'Missing required "gitHttpServer.root" config option' )
9+ if ( ! config . route ) throw new Error ( 'Missing required "gitHttpServer.route" config option' )
10+ if ( ! config . route . startsWith ( '/' ) ) throw new Error ( '"gitHttpServer.route" must start with a "/"' )
11+ // TODO: Make this configurable in karma.conf.js
12+ var f = fixturez ( config . root , { root : process . cwd ( ) } )
13+
14+ function getGitDir ( req ) {
15+ var u = url . parse ( req . url )
16+ if ( u . pathname . startsWith ( config . route ) ) {
17+ if ( req . method === 'GET' && u . pathname . endsWith ( '/info/refs' ) ) {
18+ let gitdir = u . pathname . replace ( config . route , '' ) . replace ( / \/ i n f o \/ r e f s $ / , '' ) . replace ( / ^ \/ / , '' )
19+ let fixtureName = path . basename ( gitdir )
20+ return f . find ( fixtureName )
21+ }
22+ if ( req . method === 'POST' && req . headers [ 'content-type' ] === 'application/x-git-upload-pack-request' ) {
23+ let gitdir = u . pathname . replace ( config . route , '' ) . replace ( / \/ g i t - u p l o a d - p a c k $ / , '' ) . replace ( / ^ \/ / , '' )
24+ let fixtureName = path . basename ( gitdir )
25+ return f . find ( fixtureName )
26+ }
27+ if ( req . method === 'POST' && req . headers [ 'content-type' ] === 'application/x-git-receive-pack-request' ) {
28+ let gitdir = u . pathname . replace ( config . route , '' ) . replace ( / \/ g i t - r e c e i v e - p a c k $ / , '' ) . replace ( / ^ \/ / , '' )
29+ let fixtureName = path . basename ( gitdir )
30+ return f . copy ( fixtureName )
31+ }
32+ }
33+ return null
1634 }
17- return null
18- }
1935
20- module . exports = function factory ( ) {
2136 return function middleware ( req , res , next ) {
2237 var gitdir = getGitDir ( req ) ;
2338 if ( gitdir == null ) return next ( ) ;
@@ -36,3 +51,7 @@ module.exports = function factory() {
3651 } ) ) . pipe ( res ) ;
3752 }
3853}
54+
55+ factory . $inject = [ 'config.gitHttpServer' ]
56+
57+ module . exports = factory
0 commit comments