Repository: saucelabs/node-saucelabs (js)

Issue:
Full region name responds with DNS error
**Short summary of what the user sees:**
If a user tries to to use a region name that is not the "short" version it will fail 

**Exact steps to reproduce the problem:**
1. Download latest version of saucelabs module (at time of bug report 2.1.8)
2. Try running any command via CLI or as module with a region that is not eu or us.
Example:
sl listJobs $SAUCE_USERNAME --limit 5 --region us-central-1

*Isolation: What must be true for it to happen? When does it not happen?*
Seems to only happen if you use the full region name.

**Links to Relevant Test Archives or Logs:**
```
➜  ~ sl listJobs $SAUCE_USERNAME --limit 5 --region us-central-1
{ Error: getaddrinfo ENOTFOUND us-west-1.saucelabs.com us-west-1.saucelabs.com:443
    at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:57:26)
  errno: 'ENOTFOUND',
  code: 'ENOTFOUND',
  syscall: 'getaddrinfo',
  hostname: 'us-west-1.saucelabs.com',
  host: 'us-west-1.saucelabs.com',
  port: 443 }
➜  ~ sl listJobs $SAUCE_USERNAME --limit 5 --region us-west-1
{ Error: getaddrinfo ENOTFOUND us-west-1.saucelabs.com us-west-1.saucelabs.com:443
    at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:57:26)
  errno: 'ENOTFOUND',
  code: 'ENOTFOUND',
  syscall: 'getaddrinfo',
  hostname: 'us-west-1.saucelabs.com',
  host: 'us-west-1.saucelabs.com',
  port: 443 }
```

**What attempts were made to find the root cause so far?**
This might be due to how we use the REGION_MAPPINGS and how we determine the correct DC.
https://github.com/saucelabs/node-saucelabs/blob/656b8e770c44f720dfe0b4b47b7bdf2d5d5df9bc/src/constants.js#L61
https://github.com/saucelabs/node-saucelabs/blob/656b8e770c44f720dfe0b4b47b7bdf2d5d5df9bc/src/utils.js#L43-L51

us-west-1.saucelabs.com doesn't work (not sure if intended) but eu-central-1.saucelabs.com does.
We could have us-west-1 default to saucelabs.com or app.saucelabs.com and have eu-central-1 just append the name.


Pull request:
Enable long DC name input
Fixes #56 
Makes it possible to enter the region as a short name (eu) or as long name (eu-central-1).
It adds a conditional to avoid using us-west-1.saucelabs.com since this is not an active subdomain at the moment and instead defaults to saucelabs.com

Patch:
diff --git a/src/constants.js b/src/constants.js
index 8ddc078..7e8ad3b 100644
--- a/src/constants.js
+++ b/src/constants.js
@@ -60,7 +60,9 @@ export const DEFAULT_OPTIONS = {
 
 export const REGION_MAPPING = {
     'us': 'us-west-1.', // default endpoint
-    'eu': 'eu-central-1.'
+    'eu': 'eu-central-1.',
+    'us-west-1': 'us-west-1.',
+    'eu-central-1': 'eu-central-1.'
 }
 
 export const SYMBOL_INSPECT = Symbol.for('nodejs.util.inspect.custom')
diff --git a/src/utils.js b/src/utils.js
index bb832e3..acd659a 100644
--- a/src/utils.js
+++ b/src/utils.js
@@ -47,6 +47,8 @@ export function getSauceEndpoint (hostname, region, headless, protocol = 'https:
         locale = hostname.includes('testobject') ? '' : locale
     } else if (!headless && region === 'us') {
         locale = ''
+    } else if (locale === 'us-west-1.') { // us-west-1 is currently not a valid endpoint for Sauce
+        locale = ''
     }
 
     return protocol + subdomain + locale + hostname


Test patch:
diff --git a/tests/utils.test.js b/tests/utils.test.js
index 8958a7f..a67f201 100644
--- a/tests/utils.test.js
+++ b/tests/utils.test.js
@@ -8,12 +8,16 @@ test('createHMAC', async () => {
 test('getSauceEndpoint', () => {
     expect(getSauceEndpoint('saucelabs.com', 'us', false))
         .toBe('https://saucelabs.com')
+    expect(getSauceEndpoint('saucelabs.com', 'us-west-1', false))
+        .toBe('https://saucelabs.com')
     expect(getSauceEndpoint('saucelabs.com', 'I_DONT_EXIST', false))
-        .toBe('https://us-west-1.saucelabs.com')
+        .toBe('https://saucelabs.com')
     expect(getSauceEndpoint('saucelabs.com', 'us', false, 'http://'))
         .toBe('http://saucelabs.com')
     expect(getSauceEndpoint('saucelabs.com', 'eu', false))
         .toBe('https://eu-central-1.saucelabs.com')
+    expect(getSauceEndpoint('saucelabs.com', 'eu-central-1', false))
+        .toBe('https://eu-central-1.saucelabs.com')
     expect(getSauceEndpoint('saucelabs.com', 'us', true))
         .toBe('https://us-east-1.saucelabs.com')
     expect(getSauceEndpoint('api.saucelabs.com', 'us', false))