diff --git a/docs/docs/user-guide/using-proxy-url.md b/docs/docs/user-guide/using-proxy-url.md index f69ba65a6a1..452e7a18049 100644 --- a/docs/docs/user-guide/using-proxy-url.md +++ b/docs/docs/user-guide/using-proxy-url.md @@ -1,18 +1,19 @@ -# Configure CLI for Microsoft 365 to use Proxy +# Configure CLI for Microsoft 365 to use a proxy -If you are behind a corporate proxy, you may need to configure the CLI for Microsoft 365 to use a proxy server to access Microsoft 365 services. You can configure a proxy URL using the [cli config set](/docs/docs/cmd/cli/config/config-set.md) command. +If you are behind a corporate proxy, you'll have to configure CLI for Microsoft 365 to use a proxy server to access Microsoft 365 services. You can configure a proxy URL using the [cli config set](/docs/docs/cmd/cli/config/config-set.md) command. ## Understanding Proxy URL Before configuring the proxy, it's important to understand the different parts of a proxy URL. A proxy URL typically consists of the following elements: -- **Protocol**: The protocol used by the proxy server, such as HTTP, HTTPS, or SOCKS. -- **Username and Password**: If the proxy server requires authentication, you will need to provide a username and password. -- **Host**: The hostname or IP address of the proxy server. -- **Port number**: The port number on which the proxy server is listening. Defaults to 80 when not provided. +- **protocol**: The protocol used by the proxy server, such as `HTTP`, `HTTPS`, or `SOCKS` +- **username and password**: if the proxy server requires authentication, you will need to provide a username and password +- **host**: the hostname or IP address of the proxy server +- **port number**: the port number on which the proxy server is listening. Defaults to 443 for the `HTTPS` protocol, otherwise it defaults to 80 when not provided ## Configuring Proxy URL -To configure CLI for Microsoft 365 to use a proxy, you need to execute the m365 cli config set command with the following syntax: + +To configure CLI for Microsoft 365 to use a proxy, you need to execute the `m365 cli config set` command with the following syntax: `m365 cli config set --key proxyUrl --value 'http://username:password@proxy.contoso.com:8080'` diff --git a/src/request.spec.ts b/src/request.spec.ts index 2f4678d5bac..59b6608f310 100644 --- a/src/request.spec.ts +++ b/src/request.spec.ts @@ -437,6 +437,26 @@ describe('Request', () => { }); }); + it('returns response of a successful GET request, with a proxy url and defaults port to 443', (done) => { + const config = Cli.getInstance().config as Configstore; + sinon.stub(config, 'get').callsFake(() => 'https://proxy.contoso.com'); + + sinon.stub(_request as any, 'req').callsFake((options) => { + _options = options; + return Promise.resolve({ data: {} }); + }); + + _request + .get({ + url: 'https://contoso.sharepoint.com/' + }) + .then(() => { + done(); + }, (err) => { + done(err); + }); + }); + it('returns response of a successful GET request, with a proxy url with username and password', (done) => { const config = Cli.getInstance().config as Configstore; sinon.stub(config, 'get').callsFake(() => 'http://username:password@proxy.contoso.com:8080'); diff --git a/src/request.ts b/src/request.ts index 9c24c85ad05..6b931f116b9 100644 --- a/src/request.ts +++ b/src/request.ts @@ -232,7 +232,7 @@ class Request { private createProxyConfigFromString(url: string): AxiosProxyConfig { const parsedUrl = new URL(url); const hostname = parsedUrl.hostname; - const port = parsedUrl.port || 80; + const port = parsedUrl.port || (url.toLowerCase().startsWith('https') ? 443 : 80); let authObject = null; if (parsedUrl.username && parsedUrl.password) { authObject = {