Repository: jquery/jquery
File: test/middleware-mockserver.js (JavaScript)

Code before:
	},
	cspClean: function( req, resp ) {
		cspLog = "";
		resp.writeHead( 200 );
		resp.end();
	},
	errorWithScript: function( req, resp ) {
		if ( req.query.withScriptContentType ) {
			resp.writeHead( 404, { "Content-Type": "application/javascript" } );
		} else {
			resp.writeHead( 404 );
		}
		if ( req.query.callback ) {
			resp.end( req.query.callback + "( {\"status\": 404, \"msg\": \"Not Found\"} )" );
		} else {
			resp.end( "QUnit.assert.ok( false, \"Mock return erroneously executed\" );" );
		}
	},
	cors: function( req, resp ) {
		if ( typeof req.headers[ "origin" ] != "undefined" ) {
			resp.setHeader( "Access-Control-Allow-Methods", "GET" );
			resp.setHeader( "Access-Control-Allow-Origin", req.headers[ "origin" ] );
			resp.end( "corsCallback( true )" );
		} else {
			resp.end( "corsCallback( false )" );
		}
	}
};
var handlers = {
	"test/data/mock.php": function( req, resp, next ) {
		if ( !mocks[ req.query.action ] ) {
			resp.writeHead( 400 );
			resp.end( "Invalid action query.\n" );
			console.log( "Invalid action query:", req.method, req.url );
			return;
		}
		mocks[ req.query.action ]( req, resp, next );
	},
	"test/data/support/csp.log": function( req, resp ) {
		resp.writeHead( 200 );
		resp.end( cspLog );
	},
	"test/data/404.txt": function( req, resp ) {
		resp.writeHead( 404 );
		resp.end( "" );
	}
};

/**
 * Connect-compatible middleware factory for mocking server responses.
 * Used by Ajax unit tests when run via Karma.

Review comment (suggestion):
Now that I look at it, we already have a `script` entry, I'd rather add sending CORS headers there as an option if the `cors` query parameter is set, similarly to how it's done in the `json` entry. Similar changes need to be done in `mock.php`.

Also, our current code conventions are using Title-Case header style in `mock.php` but all-lowercase in `middleware-mockserver.js`, please adjust that.

Changes I expect in `middleware-mockserver.js`:
```diff
 	script: function( req, resp ) {
 		if ( req.query.header === "ecma" ) {
 			resp.writeHead( 200, { "content-type": "application/ecmascript" } );
 		} else if ( req.query.header ) {
 			resp.writeHead( 200, { "content-type": "text/javascript" } );
 		} else {
 			resp.writeHead( 200, { "content-type": "text/html" } );
 		}
-		resp.end( "QUnit.assert.ok( true, \"mock executed\" );" );
+
+		if ( req.query.cors ) {
+			resp.writeHead( 200, { "access-control-allow-origin": "*" } );
+		}
+
+		if ( req.query.callback ) {
+			resp.end( req.query.callback + "(" + JSON.stringify( {
+				headers: req.headers
+			} ) + ")" );
+		} else {
+			resp.end( "QUnit.assert.ok( true, \"mock executed\" );" );
+		}
	},
```
In this way, you'll get all headers returned and you can inspect what you want. This also makes it possible to add more data in the future if needed.

The updated URL would be `mock.php?action=script&callback=corsCallback`.

Code after:
			"Content-Type": "text/html",
			"Content-Security-Policy": "script-src 'nonce-jquery+hardcoded+nonce'; report-uri /base/test/data/mock.php?action=cspLog"
		} );
		var body = fs.readFileSync(
			__dirname + "/data/csp-nonce" + testParam + ".html" ).toString();
		resp.end( body );
	},
	cspAjaxScript: function( req, resp ) {
		resp.writeHead( 200, {
			"Content-Type": "text/html",
			"Content-Security-Policy": "script-src 'self'; report-uri /base/test/data/mock.php?action=cspLog"
		} );
		var body = fs.readFileSync(
			__dirname + "/data/csp-ajax-script.html" ).toString();
		resp.end( body );
	},
	cspLog: function( req, resp ) {
		cspLog = "error";
		resp.writeHead( 200 );
		resp.end();
	},
	cspClean: function( req, resp ) {
		cspLog = "";
		resp.writeHead( 200 );
		resp.end();
	},
	errorWithScript: function( req, resp ) {
		if ( req.query.withScriptContentType ) {
			resp.writeHead( 404, { "Content-Type": "application/javascript" } );
		} else {
			resp.writeHead( 404 );
		}
		if ( req.query.callback ) {
			resp.end( req.query.callback + "( {\"status\": 404, \"msg\": \"Not Found\"} )" );
		} else {
			resp.end( "QUnit.assert.ok( false, \"Mock return erroneously executed\" );" );
		}
	}
};
var handlers = {
	"test/data/mock.php": function( req, resp, next ) {
		if ( !mocks[ req.query.action ] ) {
			resp.writeHead( 400 );
			resp.end( "Invalid action query.\n" );
			console.log( "Invalid action query:", req.method, req.url );
			return;
		}
		mocks[ req.query.action ]( req, resp, next );
	},
	"test/data/support/csp.log": function( req, resp ) {
		resp.writeHead( 200 );