ladjs / supertest

🕷 Super-agent driven library for testing node.js HTTP servers using a fluent API. Maintained for @forwardemail, @ladjs, @spamscanner, @breejs, @cabinjs, and @lassjs.
MIT License
13.84k stars 759 forks source link

Request Domain from specific IP #632

Open kellertobias opened 4 years ago

kellertobias commented 4 years ago

We are using supertest to test the API of an app that serves multiple Domains. Depending on the domain you request, you get different results. I want to tell supertest to request send the request to a specific IP and Port and requesting the data from a given Domain.

e.g. Ask 127.0.0.1:3000 to serve `mydomain.com/api/test.json``

how would I do that?

jimbo8098 commented 4 years ago

Supposedly (according to superagent's docs at https://visionmedia.github.io/superagent/, look for "Forcing specific connection IP address") it should be possible to use:

request("domain.com")
    .get("/")
    .connect("127.0.0.1")
    ...

However, I tried this a few moments ago and had no success. I would love to know how to do that.

Meantime, one workaround might be to override your hosts file but that's extremely janky.

jimbo8098 commented 4 years ago

A workaround, though not using supertest, is to use superagent directly:

var mocha = require('mocha');
var forEach = require('mocha-each');
var assert = require("assert");
var superagent = require('superagent')

var ports = [
    [80,"http"]
]

describe("Responds as expected", () => {

    forEach(ports)
    .it("Should respond on port %d using %s",(port,schema) => {
        return superagent
            .get("http://google.com")
            .connect("127.0.0.1")
            .then(function(res) {
                    assert.equal(res.statusCode,200,"Status should be 200")
                    assert.notEqual(res.text.indexOf("Running Profcesses"),-1,"Expect Running Processes to be within the page downloaded")
            })

    });
})

This does what I need, connecting to 127.0.0.1 instead. This is part of the superagent test which I believe should allow such a command to run but there you go 🤷