Visitar URL original
fix: Unclear error message is thrown when specifying an empty proxy authorization by joaoGabriel55 · Pull Request #6314 · axios/axios · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
fix: add AxiosError to Invalid proxy authorization
  • Loading branch information
João Gabriel Quaresma de Almeida João Gabriel Quaresma de Almeida
João Gabriel Quaresma de Almeida authored and João Gabriel Quaresma de Almeida committed Mar 22, 2024
commit bc138ae173cc4acbaf131f70bc13c2be3109b5b6
10 changes: 6 additions & 4 deletions lib/adapters/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,13 @@ function setProxy(options, configProxy, location) {
// Support proxy auth object form
if (proxy.auth.username || proxy.auth.password) {
proxy.auth = (proxy.auth.username || '') + ':' + (proxy.auth.password || '');

const base64 = Buffer.from(proxy.auth, 'utf8').toString('base64');

options.headers['Proxy-Authorization'] = 'Basic ' + base64;
} else {
throw new AxiosError('Invalid proxy authorization', AxiosError.ERR_BAD_OPTION, { proxy });
}
const base64 = Buffer
.from(proxy.auth, 'utf8')
.toString('base64');
options.headers['Proxy-Authorization'] = 'Basic ' + base64;
}

options.headers.host = options.hostname + (options.port ? ':' + options.port : '');
Expand Down
23 changes: 23 additions & 0 deletions test/unit/adapters/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -1401,6 +1401,29 @@ describe('supports http with nodejs', function () {
});
});

describe('when invalid proxy options are provided', function () {
it('should throw error', function () {
const proxy = {
protocol: "http:",
host: "hostname.abc.xyz",
port: 3300,
auth: {
username: "",
password: "",
}
};

return axios.get("https://test-domain.abc", {proxy})
Comment thread
jasonsaayman marked this conversation as resolved.
Resolved
.then(function(){
fail('Does not throw');
Comment thread
jasonsaayman marked this conversation as resolved.
Resolved
}, function (error) {
Comment thread
jasonsaayman marked this conversation as resolved.
Resolved
assert.strictEqual(error.message, 'Invalid proxy authorization')
assert.strictEqual(error.code, 'ERR_BAD_OPTION')
assert.deepStrictEqual(error.config.proxy, proxy)
Comment thread
jasonsaayman marked this conversation as resolved.
Resolved
})
});
});

context('different options for direct proxy configuration (without env variables)', () => {
const destination = 'www.example.com';

Expand Down