Permalink
Browse files

add .isCrossDomain(url)

  • Loading branch information...
1 parent 73666c2 commit e04a3e6dda3074e99d688222c93ec1325a1a3791 @tj tj committed Oct 10, 2012
Showing with 61 additions and 4 deletions.
  1. +4 −1 Makefile
  2. +5 −0 Readme.md
  3. +3 −0 component.json
  4. +16 −0 index.js
  5. +33 −3 test/tests.js
View
5 Makefile
@@ -8,4 +8,7 @@ components: component.json
clean:
rm -fr build components template.js
-.PHONY: clean
+test:
+ serve .
+
+.PHONY: clean test
View
5 Readme.md
@@ -20,6 +20,7 @@ yields:
{
hash: ""
host: "example.com:3000"
+ port: 3000,
hostname: "example.com"
href: "http://example.com:3000/store/shoes?sort=desc"
pathname: "/store/shoes"
@@ -43,6 +44,10 @@ yields:
Check if the given url `string` is relative.
+### url.isCrossDomain(string)
+
+ Check if the given url `string` is cross-domain.
+
## Note
This url "parser" uses an __A__ tag, this means that when
View
3 component.json
@@ -4,6 +4,9 @@
"description": "url parser",
"version": "0.0.1",
"keywords": ["url", "utility", "parser"],
+ "development": {
+ "component/assert": "*"
+ },
"scripts": [
"index.js"
]
View
16 index.js
@@ -13,6 +13,7 @@ exports.parse = function(url){
return {
href: a.href,
host: a.host,
+ port: a.port,
hash: a.hash,
hostname: a.hostname,
pathname: a.pathname,
@@ -46,4 +47,19 @@ exports.isAbsolute = function(url){
exports.isRelative = function(url){
return ! exports.isAbsolute(url);
+};
+
+/**
+ * Check if `url` is cross domain.
+ *
+ * @param {String} url
+ * @return {Boolean}
+ * @api public
+ */
+
+exports.isCrossDomain = function(url){
+ url = exports.parse(url);
+ return url.hostname != location.hostname
+ || url.port != location.port
+ || url.protocol != location.protocol;
};
View
36 test/tests.js
@@ -1,10 +1,40 @@
var url = require('url')
+ , assert = require('component-assert')
, parse = url.parse;
-function assert(expr) {
- if (!expr) throw new Error('assertion error');
-}
+describe('url.isCrossDomain(str)', function(){
+ describe('when everything matches', function(){
+ it('should return false', function(){
+ assert(false == url.isCrossDomain('/hello/world'));
+ assert(false == url.isCrossDomain('http://localhost:3000/hello/world'));
+ })
+ })
+
+ describe('when hostname mismatches', function(){
+ it('should return true', function(){
+ assert(true == url.isCrossDomain('https://example.com:3000/hello/world'));
+ })
+ })
+
+ describe('when protocol mismatches', function(){
+ it('should return true', function(){
+ assert(true == url.isCrossDomain('https://localhost:3000/hello/world'));
+ })
+ })
+
+ describe('when port mismatches', function(){
+ it('should return true', function(){
+ assert(true == url.isCrossDomain('http://localhost:3001/hello/world'));
+ })
+ })
+
+ describe('when port is missing', function(){
+ it('should return true', function(){
+ assert(true == url.isCrossDomain('http://localhost/hello/world'));
+ })
+ })
+})
describe('url.isAbsolute(str)', function(){
it('should support scheme://', function(){

0 comments on commit e04a3e6

Please sign in to comment.