-
Notifications
You must be signed in to change notification settings - Fork 1.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Ability to inject custom middleware #1612
Ability to inject custom middleware #1612
Comments
@shanear what issue you want resolve by custom middleware? Thanks |
We're using karma to test an application connected to an API. We're in a large organization, and we have little control over this API. We've been using middleware to:
It's been an effective solution for running the application locally, and it's been nice to use the same solution to run the automated tests locally too (via test'em). |
@shanear thanks for adding this feature, I'm having an issue getting a response from the server and parsing data from the request. Wondering if you could provide any useful suggestions.
Any info you could offer or example repos would be great. //config
files: [
'http://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js',
testPath
],
preprocessors,
middleware: ['custom', 'another', 'last'],
plugins: [
'karma-*',
{
'middleware:custom': ['factory', function(config) {
return function(request, response, next) {
console.log('RESPONSE1*********');
next();
};
}],
'middleware:another': ['factory', function(config) {
return function(request, response, next) {
console.log('RESPONSE2**********');
next();
};
}],
'middleware:last': ['factory', function(config) {
return function(request, response, next) {
console.log('RESPONSE3**********', request._parsedUrl);
response.writeHead(222);
return response.end('Hello There');
};
}]
}
], //spec
it('should make a request', (cb) => {
const defer = $.get({
url: 'http:localhost:9876/whatevs?stuff=things'
});
defer.then((data) => {
console.log('DATA', data);
cb();
}, (err) => {
console.log('ERR', err);
cb();
});
}); #terminal output
RESPONSE1*********
RESPONSE2**********
RESPONSE3********** Url {
protocol: null,
slashes: null,
auth: null,
host: null,
port: null,
hostname: null,
hash: null,
search: null,
query: null,
pathname: '/[object%20Object]',
path: '/[object%20Object]',
href: '/[object%20Object]',
_raw: '/[object%20Object]' } |
hi @dtothefp, your middleware configuration looks fine. I'm wondering about the spec. It seems like the url might not be correct. Are you sure you need to precede it with Unfortunately the application I use this feature in is closed source, but the karma test code may help. You can check it out here: also we use this function for our tests and it works fine:
Hope that helps! |
@shanear thanks yeh I was looking at your tests before. Ends up that making the request with On another note, it seems the middleware would be more useful if it followed the same syntax as projects such as Not sure if this is possible though with the dependency injection system that karma is using? ex middleware: ['custom'],
plugins: [
'karma-*',
{
'middleware:custom': ['factory', function(config) {
return [
function firstMiddleware(request, response, next) {
if (/base/.test(request.url)) {
response.writeHead(222);
return response.end('hello!!!');
}
next();
},
function firstMiddleware(request, response, next) {
response.writeHead(222);
return response.end('gotcha!!!');
}
]
}]
}
], |
@dtothefp any luck on overwriting karma's middleware workflow? Considering that any custom middleware will be executed after all standard middlewares:
And that
How one can have his/her middleware interpreting regular files? |
We're trying to switch from test'em to karma, and one big issue is the inability to inject custom middleware into the karma test server.
Here is where test'em allows middleware injection via a configuration option:
https://github.com/airportyh/testem/blob/e70cbd7f67444b1a9070102d58c6789d39946529/lib/server/index.js#L149
The karma webserver has a mechanism that's far more obtuse and restrictive (also, the comments make it seem like it's on the chopping block):
https://github.com/karma-runner/karma/blob/master/lib/web-server.js#L65
Is there any plans to allow something like this? If there is, I could whip something up.
The text was updated successfully, but these errors were encountered: