Proxy with custom response Headers

319 views
Skip to first unread message

Rohit Goyal

unread,
Oct 18, 2014, 7:18:33 AM10/18/14
to mounteban...@googlegroups.com
I want to proxy a HTTP URL which I could do successfully.

But my requirement also says that with response, I need to add some additional header. It means once my backend sends response back to Mountebank, before Mountebank sends response back to consumer/client application, it should add some custom header. Please suggest how it's possible? 

I couldn't find any option with stub proxy to customize received server response before sending back to client application.

Brandon Byars

unread,
Oct 18, 2014, 10:24:43 AM10/18/14
to Rohit Goyal, mountebank-discuss
You're right Rohit - that is not currently supported.  It sounds like a good suggestion; I will add it as time allows.
-Brandon

--
You received this message because you are subscribed to the Google Groups "mountebank-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mountebank-disc...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Brandon Byars

unread,
Oct 18, 2014, 11:19:41 AM10/18/14
to Rohit Goyal, mountebank-discuss
Actually I spoke too soon.  You can do this with the inject resolver, but it's going to be complicated.  In general, the inject resolver is used to support extending mountebank with good ideas that I simply never thought about to buy time to implement those good ideas in an easier way.

The inject documentation demonstrates creating a proxy: http://www.mbtest.org/docs/api/injection.  The function, copied and slightly modified from the docs, should come close to what you want; it will behave similarly to a proxyOnce proxy:

function (request, state, logger, callback) {
    var cacheKey = request.method + ' ' + request.path;

    if (typeof state.requests === 'undefined') {
        state.requests = {};
    }

    if (state.requests[cacheKey]) {
        logger.info('Using previous response');
        callback(state.requests[cacheKey]);
    }

    var http = require('http'),
        options = {
            method: request.method,
            hostname: 'localhost',
            port: 5555,
            path: request.path,
            headers: request.headers
        },
        httpRequest = http.request(options, function (response) {
            var body = '';
            response.setEncoding('utf8');
            response.on('data', function (chunk) {
                body += chunk;
            });
            response.on('end', function () {
                var stubResponse = {
                        statusCode: response.statusCode,
                        headers: response.headers,
                        body: body
                    };

                // Add custom header
                stubResponse.headers['X-Custom'] = 'Testing...'

                logger.info('Successfully proxied: ' + JSON.stringify(stubResponse));
                state.requests[cacheKey] = stubResponse;
                callback(stubResponse);
            });
        });
    httpRequest.end();
}

Brandon Byars

unread,
Jan 4, 2015, 12:14:04 AM1/4/15
to Rohit Goyal, mountebank-discuss
v1.2.0 now supports a simpler solution to this using the new decorate behavior.  This particular example is documented on http://www.mbtest.org/docs/api/behaviors.
-Brandon
Reply all
Reply to author
Forward
0 new messages