http-request icon indicating copy to clipboard operation
http-request copied to clipboard

dev host internal set custom DNS programatically

Open braghome opened this issue 10 years ago • 4 comments

On the internal RESTful testing sometimes their is a need to replace and entry in the /etc/hosts file programmatically for example

169.34.0.39 auth.api.dev.mycompany.com

if I get the run my tests on jenkins and can't change the hosts file their then I want to add something like

InetAddress address = null;
try {
        address = InetAddress.getByAddress("http://auth.api.dev.mycompany.com", new byte[] {169, 34, 0, 39});
    } catch (UnknownHostException e) {
        LOGGER.error(LogMessages.READOBJECT.toString(), e.getMessage());
    }
return address == null ? EnvMyCompany.baseUrl() : address.getHostName();

what would be a quick way to bind a host to a custom ip with HttpRequest

braghome avatar Jan 24 '14 21:01 braghome

I think you might want to try using a custom connection factory.

That way you could perform the address checks there and return a connection opened to the right URL.

kevinsawicki avatar Jan 25 '14 01:01 kevinsawicki

I tried this

class DevBaseUrlResolve implements BaseUrl, ConnectionFactory {
    private static final Logger LOGGER = LoggerFactory.getLogger(DevBaseUrlResolve.class);
    @Resource private Functions functions;

    /**
     * {@inheritDoc}
     */
    @Override
    public String resovleBaseUrl() {
        final AbstractPatterns patterns = EnvMyCompany.identity(AbstractPatterns.class, "patterns");
        InetAddress address = null;
        if (patterns.isValidPattern(EnvMyCompany.baseUrl(), "((ht|f)tp(s?))\\://(?:\\d{1,3}\\.){3}\\d{1,3}")) {
            try {
                address = InetAddress.getByAddress("http://auth.api.dev.mycompany.com", functions.parseNumberIpAddress(EnvMyCompany.baseUrl(), "\\."));
            } catch (UnknownHostException e) {
                LOGGER.error(LogMessages.READOBJECT.toString(), e.getMessage());
            }
        }
        return address == null ? EnvMyCompany.baseUrl() : address.getHostName();
    }

    @Override
    public HttpURLConnection create(final URL url) throws IOException {
        return handleApiDev(url);
    }

    private HttpURLConnection handleApiDev(final URL url) throws MalformedURLException,
            APIException, IOException {
        URL dev = new URL("http://169.34.0.39");
        if (url.equals(dev)) {
            dev = new URL(resovleBaseUrl());
        } else {
            throw new APIException("Base url for DEV is incorrect");
        }

        return (HttpURLConnection) dev.openConnection();
    }

    @Override
    public HttpURLConnection create(final URL url, final Proxy proxy) throws IOException {
        return handleApiDev(url);
    }
}

I get to wire this here

     /**
     * {@inheritDoc}
     */
    @Override
    public HttpRequest postFormRequest(final String payload, final String completeUrl,
            final String token) {
        HttpRequest request = null;
        try {
            final URL wsaAuth = new URL(completeUrl);
            request = HttpRequest.post(wsaAuth)
                            .contentType(HttpRequest.CONTENT_TYPE_FORM)
                            .header(HeaderConst.AGENT_KEY.toString(), HeaderConst.AGENT_VAL.toString())
                            .acceptGzipEncoding()
                            .uncompress(true)
                            .authorization(token)
                            .acceptJson();
        } catch (HttpRequestException e) {
            LOGGER.error(Purchase.FAILEDREQUESTMESSAGE.toString(), e.getMessage());
        } catch (MalformedURLException e) {
            LOGGER.error(Purchase.BADURLMESSAGE.toString(), e.getMessage());
        }
        request.setConnectionFactory(new DevBaseUrlResolve());
        return request.send(payload);
    }

however the problem is this, and which is driving me crazy when I check charles proxy

POST http://169.34.0.39/oauth2/token HTTP/1.1 User-Agent: Java/6_apicore_test13 Content-Type: application/x-www-form-urlencoded Accept: application/json Authorization: Basic someYwMbase64valueQ= Host: 169.34.0.39 Proxy-Connection: keep-alive Content-Length: 79

I need to see the header Host: http://auth.api.dev.mycompany.com instead of the incorrect value Host: 169.34.0.39

braghome avatar Jan 25 '14 07:01 braghome

Actually I just need to see the Host: 169.34.0.39 change to Host: http://auth.api.dev.mycompany.com and every can be the same

braghome avatar Jan 25 '14 08:01 braghome

So did you figure this out or are you still having issues getting it to work how you need it?

kevinsawicki avatar Jan 27 '14 22:01 kevinsawicki