How to use parseUrlIntoDomainTldPort method in Cypress

Best JavaScript code snippet using cypress

server.js

Source:server.js Github

copy

Full Screen

...557      this._remoteStrategy = 'http'558      this._remoteFileServer = null559      // set an object with port, tld, and domain properties560      // as the remoteHostAndPort561      this._remoteProps = cors.parseUrlIntoDomainTldPort(this._remoteOrigin)562      this._remoteDomainName = _.compact([this._remoteProps.domain, this._remoteProps.tld]).join('.')563      l('remoteOrigin', this._remoteOrigin)564      l('remoteHostAndPort', this._remoteProps)565      l('remoteDocDomain', this._remoteDomainName)566    }567    return this._getRemoteState()568  }569  _callRequestListeners (server, listeners, req, res) {570    return listeners.map((listener) => {571      return listener.call(server, req, res)572    })573  }574  _normalizeReqUrl (server) {575    // because socket.io removes all of our request576    // events, it forces the socket.io traffic to be577    // handled first.578    // however we need to basically do the same thing579    // it does and after we call into socket.io go580    // through and remove all request listeners581    // and change the req.url by slicing out the host582    // because the browser is in proxy mode583    const listeners = server.listeners('request').slice(0)584    server.removeAllListeners('request')585    return server.on('request', (req, res) => {586      setProxiedUrl(req)587      return this._callRequestListeners(server, listeners, req, res)588    })589  }590  _retryBaseUrlCheck (baseUrl, onWarning) {591    return ensureUrl.retryIsListening(baseUrl, {592      retryIntervals: [3000, 3000, 4000],593      onRetry ({ attempt, delay, remaining }) {594        const warning = errors.get('CANNOT_CONNECT_BASE_URL_RETRYING', {595          remaining,596          attempt,597          delay,598          baseUrl,599        })600        return onWarning(warning)601      },602    })603  }604  proxyWebsockets (proxy, socketIoRoute, req, socket, head) {605    // bail if this is our own namespaced socket.io request606    if (req.url.startsWith(socketIoRoute)) {607      if (!this._socketAllowed.isRequestAllowed(req)) {608        socket.write('HTTP/1.1 400 Bad Request\r\n\r\nRequest not made via a Cypress-launched browser.')609        socket.end()610      }611      // we can return here either way, if the socket is still valid socket.io will hook it up612      return613    }614    const host = req.headers.host615    if (host) {616      // get the protocol using req.connection.encrypted617      // get the port & hostname from host header618      const fullUrl = `${req.connection.encrypted ? 'https' : 'http'}://${host}`619      const { hostname, protocol } = url.parse(fullUrl)620      const { port } = cors.parseUrlIntoDomainTldPort(fullUrl)621      const onProxyErr = (err, req, res) => {622        return debug('Got ERROR proxying websocket connection', { err, port, protocol, hostname, req })623      }624      return proxy.ws(req, socket, head, {625        secure: false,626        target: {627          host: hostname,628          port,629          protocol,630        },631        agent,632      }, onProxyErr)633    }634    // we can't do anything with this socket...

Full Screen

Full Screen

server-base.js

Source:server-base.js Github

copy

Full Screen

...328            this._remoteStrategy = 'http';329            this._remoteFileServer = null;330            // set an object with port, tld, and domain properties331            // as the remoteHostAndPort332            this._remoteProps = network_1.cors.parseUrlIntoDomainTldPort(this._remoteOrigin);333            // @ts-ignore334            this._remoteDomainName = lodash_1.default.compact([this._remoteProps.domain, this._remoteProps.tld]).join('.');335            l('remoteOrigin', this._remoteOrigin);336            l('remoteHostAndPort', this._remoteProps);337            l('remoteDocDomain', this._remoteDomainName);338        }339        return this._getRemoteState();340    }341    proxyWebsockets(proxy, socketIoRoute, req, socket, head) {342        // bail if this is our own namespaced socket.io request343        if (req.url.startsWith(socketIoRoute)) {344            if (!this.socketAllowed.isRequestAllowed(req)) {345                socket.write('HTTP/1.1 400 Bad Request\r\n\r\nRequest not made via a Cypress-launched browser.');346                socket.end();347            }348            // we can return here either way, if the socket is still valid socket.io will hook it up349            return;350        }351        const host = req.headers.host;352        if (host) {353            // get the protocol using req.connection.encrypted354            // get the port & hostname from host header355            const fullUrl = `${req.connection.encrypted ? 'https' : 'http'}://${host}`;356            const { hostname, protocol } = url_1.default.parse(fullUrl);357            const { port } = network_1.cors.parseUrlIntoDomainTldPort(fullUrl);358            const onProxyErr = (err, req, res) => {359                return debug('Got ERROR proxying websocket connection', { err, port, protocol, hostname, req });360            };361            return proxy.ws(req, socket, head, {362                secure: false,363                target: {364                    host: hostname,365                    port,366                    protocol,367                },368                agent: network_1.agent,369            }, onProxyErr);370        }371        // we can't do anything with this socket...

Full Screen

Full Screen

cors_spec.js

Source:cors_spec.js Github

copy

Full Screen

...3describe('lib/util/cors', () => {4  context('.parseUrlIntoDomainTldPort', () => {5    beforeEach(function () {6      this.isEq = (url, obj) => {7        expect(cors.parseUrlIntoDomainTldPort(url)).to.deep.eq(obj)8      }9    })10    it('parses https://www.google.com', function () {11      this.isEq('https://www.google.com', {12        port: '443',13        domain: 'google',14        tld: 'com',15      })16    })17    it('parses http://localhost:8080', function () {18      this.isEq('http://localhost:8080', {19        port: '8080',20        domain: '',21        tld: 'localhost',22      })23    })24    it('parses http://app.localhost:8080', function () {25      this.isEq('http://app.localhost:8080', {26        port: '8080',27        domain: 'app',28        tld: 'localhost',29      })30    })31    it('parses http://app.localhost.dev:8080', function () {32      this.isEq('http://app.localhost.dev:8080', {33        port: '8080',34        domain: 'localhost',35        tld: 'dev',36      })37    })38    it('parses http://app.local:8080', function () {39      this.isEq('http://app.local:8080', {40        port: '8080',41        domain: 'app',42        tld: 'local',43      })44    })45    // public suffix example of a private tld46    it('parses https://example.herokuapp.com', function () {47      this.isEq('https://example.herokuapp.com', {48        port: '443',49        domain: 'example',50        tld: 'herokuapp.com',51      })52    })53    it('parses http://www.local.nl', function () {54      this.isEq('http://www.local.nl', {55        port: '80',56        domain: 'local',57        tld: 'nl',58      })59    })60    // https://github.com/cypress-io/cypress/issues/371761    it('parses http://dev.classea12.beta.gouv.fr', function () {62      this.isEq('http://dev.classea12.beta.gouv.fr', {63        port: '80',64        domain: 'beta',65        tld: 'gouv.fr',66      })67    })68    it('parses http://www.local.nl:8080', function () {69      this.isEq('http://www.local.nl:8080', {70        port: '8080',71        domain: 'local',72        tld: 'nl',73      })74    })75    it('parses 192.168.1.1:8080', function () {76      this.isEq('http://192.168.1.1:8080', {77        port: '8080',78        domain: '',79        tld: '192.168.1.1',80      })81    })82  })83  context('.urlMatchesOriginPolicyProps', () => {84    beforeEach(function () {85      this.isFalse = (url, props) => {86        expect(cors.urlMatchesOriginPolicyProps(url, props)).to.be.false87      }88      this.isTrue = (url, props) => {89        expect(cors.urlMatchesOriginPolicyProps(url, props)).to.be.true90      }91    })92    describe('domain + subdomain', () => {93      beforeEach(function () {94        this.props = cors.parseUrlIntoDomainTldPort('https://staging.google.com')95      })96      it('does not match', function () {97        this.isFalse('https://foo.bar:443', this.props)98        this.isFalse('http://foo.bar:80', this.props)99        this.isFalse('http://foo.bar', this.props)100        this.isFalse('http://staging.google.com', this.props)101        this.isFalse('http://staging.google.com:80', this.props)102        this.isFalse('https://staging.google2.com:443', this.props)103        this.isFalse('https://staging.google.net:443', this.props)104        this.isFalse('https://google.net:443', this.props)105        this.isFalse('http://google.com', this.props)106      })107      it('matches', function () {108        this.isTrue('https://staging.google.com:443', this.props)109        this.isTrue('https://google.com:443', this.props)110        this.isTrue('https://foo.google.com:443', this.props)111        this.isTrue('https://foo.bar.google.com:443', this.props)112      })113    })114    describe('public suffix', () => {115      beforeEach(function () {116        this.props = cors.parseUrlIntoDomainTldPort('https://example.gitlab.io')117      })118      it('does not match', function () {119        this.isFalse('http://example.gitlab.io', this.props)120        this.isFalse('https://foo.gitlab.io:443', this.props)121      })122      it('matches', function () {123        this.isTrue('https://example.gitlab.io:443', this.props)124        this.isTrue('https://foo.example.gitlab.io:443', this.props)125      })126    })127    describe('localhost', () => {128      beforeEach(function () {129        this.props = cors.parseUrlIntoDomainTldPort('http://localhost:4200')130      })131      it('does not match', function () {132        this.isFalse('http://localhost:4201', this.props)133        this.isFalse('http://localhoss:4200', this.props)134      })135      it('matches', function () {136        this.isTrue('http://localhost:4200', this.props)137      })138    })139    describe('app.localhost', () => {140      beforeEach(function () {141        this.props = cors.parseUrlIntoDomainTldPort('http://app.localhost:4200')142      })143      it('does not match', function () {144        this.isFalse('http://app.localhost:4201', this.props)145        this.isFalse('http://app.localhoss:4200', this.props)146      })147      it('matches', function () {148        this.isTrue('http://app.localhost:4200', this.props)149        this.isTrue('http://name.app.localhost:4200', this.props)150      })151    })152    describe('local', () => {153      beforeEach(function () {154        this.props = cors.parseUrlIntoDomainTldPort('http://brian.dev.local')155      })156      it('does not match', function () {157        this.isFalse('https://brian.dev.local:443', this.props)158        this.isFalse('https://brian.dev.local', this.props)159        this.isFalse('http://brian.dev2.local:81', this.props)160      })161      it('matches', function () {162        this.isTrue('http://jennifer.dev.local:80', this.props)163        this.isTrue('http://jennifer.dev.local', this.props)164      })165    })166    describe('ip address', () => {167      beforeEach(function () {168        this.props = cors.parseUrlIntoDomainTldPort('http://192.168.5.10')169      })170      it('does not match', function () {171        this.isFalse('http://192.168.5.10:443', this.props)172        this.isFalse('https://192.168.5.10', this.props)173        this.isFalse('http://193.168.5.10', this.props)174        this.isFalse('http://193.168.5.10:80', this.props)175      })176      it('matches', function () {177        this.isTrue('http://192.168.5.10', this.props)178        this.isTrue('http://192.168.5.10:80', this.props)179      })180    })181  })182  context('.urlMatchesOriginProtectionSpace', () => {...

Full Screen

Full Screen

cors.js

Source:cors.js Github

copy

Full Screen

...30const debug = (0, debug_1.default)('cypress:network:cors');31// match IP addresses or anything following the last .32const customTldsRe = /(^[\d\.]+$|\.[^\.]+$)/;33function getSuperDomain(url) {34    const parsed = parseUrlIntoDomainTldPort(url);35    return lodash_1.default.compact([parsed.domain, parsed.tld]).join('.');36}37exports.getSuperDomain = getSuperDomain;38function parseDomain(domain, options = {}) {39    return (0, parse_domain_1.default)(domain, lodash_1.default.defaults(options, {40        privateTlds: true,41        customTlds: customTldsRe,42    }));43}44exports.parseDomain = parseDomain;45function parseUrlIntoDomainTldPort(str) {46    let { hostname, port, protocol } = uri.parse(str);47    if (!hostname) {48        hostname = '';49    }50    if (!port) {51        port = protocol === 'https:' ? '443' : '80';52    }53    let parsed = parseDomain(hostname);54    // if we couldn't get a parsed domain55    if (!parsed) {56        // then just fall back to a dumb check57        // based on assumptions that the tld58        // is the last segment after the final59        // '.' and that the domain is the segment60        // before that61        const segments = hostname.split('.');62        parsed = {63            tld: segments[segments.length - 1] || '',64            domain: segments[segments.length - 2] || '',65        };66    }67    const obj = {};68    obj.port = port;69    obj.tld = parsed.tld;70    obj.domain = parsed.domain;71    debug('Parsed URL %o', obj);72    return obj;73}74exports.parseUrlIntoDomainTldPort = parseUrlIntoDomainTldPort;75function urlMatchesOriginPolicyProps(urlStr, props) {76    // take a shortcut here in the case77    // where remoteHostAndPort is null78    if (!props) {79        return false;80    }81    const parsedUrl = parseUrlIntoDomainTldPort(urlStr);82    // does the parsedUrl match the parsedHost?83    return lodash_1.default.isEqual(parsedUrl, props);84}85exports.urlMatchesOriginPolicyProps = urlMatchesOriginPolicyProps;86function urlMatchesOriginProtectionSpace(urlStr, origin) {87    const normalizedUrl = uri.addDefaultPort(urlStr).format();88    const normalizedOrigin = uri.addDefaultPort(origin).format();89    return lodash_1.default.startsWith(normalizedUrl, normalizedOrigin);90}...

Full Screen

Full Screen

Using AI Code Generation

copy

Full Screen

1describe('Test', function() {2  it('test', function() {3    cy.window().then(win => {4      expect(domain).to.equal('google.com')5    })6  })7})8var url = window.location.href;9var domain = url.split('/')[2];10var url = window.location.href;11var domain = url.split('/')[2];12var url = window.location.href;13var domain = url.split('/')[2];14var url = window.location.href;15var domain = url.split('/')[2];16var url = window.location.href;17var domain = url.split('/')[2];18var url = window.location.href;19var domain = url.split('/')[2];

Full Screen

Using AI Code Generation

copy

Full Screen

1cy.log(2  Cypress.parseUrlIntoDomainTldPort(3);4{5}6cy.log(7  Cypress.parseUrlIntoDomainTldPort(8);9{10}11cy.log(12  Cypress.parseUrlIntoDomainTldPort(13);14{15}16cy.log(17  Cypress.parseUrlIntoDomainTldPort(18);19{20}21cy.log(22  Cypress.parseUrlIntoDomainTldPort(23);24{25}26cy.log(27  Cypress.parseUrlIntoDomainTldPort(28);29{30}31cy.log(32  Cypress.parseUrlIntoDomainTldPort(33);34{

Full Screen

Using AI Code Generation

copy

Full Screen

1describe('Cypress test', () => {2  it('should test domain name', () => {3  })4})5describe('Cypress test', () => {6  it('should test domain name', () => {7  })8})9describe('Cypress test', () => {10  it('should test domain name', () => {11  })12})13describe('Cypress test', () => {14  it('should test domain name', () => {15  })16})17describe('Cypress test', () => {18  it('should test domain name', () => {19  })20})21describe('Cypress test', () => {22  it('should test domain name', () => {23  })24})25describe('Cypress test', () => {26  it('should test domain name', () => {27    cy.visit('https

Full Screen

Using AI Code Generation

copy

Full Screen

1const cy = require('cypress')2const { domain, tld, port } = cy.parseUrlIntoDomainTldPort(url)3console.log(domain, tld, port)4Using Cypress.Commands.add() to add a custom command5Using cy.task() to add a task6Using Cypress.Commands.add() to add a custom command7Cypress.Commands.add('parseUrlIntoDomainTldPort', (url) => {8  const { domain, tld, port } = cy.parseUrlIntoDomainTldPort(url)9  return { domain, tld, port }10})11describe('Test', () => {12  it('Test', () => {13    cy.parseUrlIntoDomainTldPort(Cypress.config().baseUrl).then((result) => {14      console.log(result)15    })16  })17})18{domain: "localhost", tld: "com", port: "3000"}19Using cy.task() to add a task20module.exports = (on, config) => {21  on('task', {22    parseUrlIntoDomainTldPort: (url) => {23      const { domain, tld, port } = cy.parseUrlIntoDomainTldPort(url)24      return { domain, tld, port }25    },26  })27}28describe('Test', () => {29  it('Test',

Full Screen

Using AI Code Generation

copy

Full Screen

1describe('Test', () => {2    it('Test', () => {3        cy.log(cy.parseUrlIntoDomainTldPort());4    });5});6Cypress.Commands.add('parseUrlIntoDomainTldPort', () => {7    const url = Cypress.config().baseUrl;8    const parsedUrl = new URL(url);9    const domain = parsedUrl.hostname;10    const port = parsedUrl.port;11    const tld = domain.split('.').pop();12    return { domain, tld, port };13});14{15}

Full Screen

Cypress Tutorial

Cypress is a renowned Javascript-based open-source, easy-to-use end-to-end testing framework primarily used for testing web applications. Cypress is a relatively new player in the automation testing space and has been gaining much traction lately, as evidenced by the number of Forks (2.7K) and Stars (42.1K) for the project. LambdaTest’s Cypress Tutorial covers step-by-step guides that will help you learn from the basics till you run automation tests on LambdaTest.

Chapters:

  1. What is Cypress? -
  2. Why Cypress? - Learn why Cypress might be a good choice for testing your web applications.
  3. Features of Cypress Testing - Learn about features that make Cypress a powerful and flexible tool for testing web applications.
  4. Cypress Drawbacks - Although Cypress has many strengths, it has a few limitations that you should be aware of.
  5. Cypress Architecture - Learn more about Cypress architecture and how it is designed to be run directly in the browser, i.e., it does not have any additional servers.
  6. Browsers Supported by Cypress - Cypress is built on top of the Electron browser, supporting all modern web browsers. Learn browsers that support Cypress.
  7. Selenium vs Cypress: A Detailed Comparison - Compare and explore some key differences in terms of their design and features.
  8. Cypress Learning: Best Practices - Take a deep dive into some of the best practices you should use to avoid anti-patterns in your automation tests.
  9. How To Run Cypress Tests on LambdaTest? - Set up a LambdaTest account, and now you are all set to learn how to run Cypress tests.

Certification

You can elevate your expertise with end-to-end testing using the Cypress automation framework and stay one step ahead in your career by earning a Cypress certification. Check out our Cypress 101 Certification.

YouTube

Watch this 3 hours of complete tutorial to learn the basics of Cypress and various Cypress commands with the Cypress testing at LambdaTest.

Run Cypress automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful