Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
N
nginx-push-stream-module
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Administrator
nginx-push-stream-module
Commits
28d810db
Commit
28d810db
authored
Aug 25, 2013
by
Wandenberg
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
wrap some javascript methods on a Utils class to make tests easier
parent
7283136d
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
271 additions
and
25 deletions
+271
-25
Rakefile
misc/Rakefile
+1
-3
pushstream.js
misc/js/pushstream.js
+14
-12
PushStreamSpec.js
misc/spec/javascripts/PushStreamSpec.js
+79
-10
UtilsSpec.js
misc/spec/javascripts/UtilsSpec.js
+177
-0
No files found.
misc/Rakefile
View file @
28d810db
...
...
@@ -43,9 +43,7 @@ begin
destiny_path
=
File
.
dirname
(
file
).
gsub
(
/.*\/js\/?/
,
File
.
expand_path
(
'pushstream/js'
,
Dir
.
tmpdir
))
FileUtils
.
mkdir_p
(
destiny_path
)
content
=
File
.
read
file
content
.
gsub!
(
'(function (window, document, undefined) {'
,
''
)
content
.
gsub!
(
'if (window.PushStream) { return; }'
,
''
)
content
.
gsub!
(
'})(window, document);'
,
''
)
content
.
gsub!
(
'window.PushStream = PushStream;'
,
"window.PushStream = PushStream;
\n
window.Utils = Utils;"
)
File
.
open
(
File
.
join
(
destiny_path
,
File
.
basename
(
file
)),
'w'
)
{
|
f
|
f
.
write
content
}
end
end
...
...
misc/js/pushstream.js
View file @
28d810db
...
...
@@ -30,6 +30,8 @@
/* prevent duplicate declaration */
if
(
window
.
PushStream
)
{
return
;
}
var
Utils
=
{};
var
days
=
[
"
Sun
"
,
"
Mon
"
,
"
Tue
"
,
"
Wed
"
,
"
Thu
"
,
"
Fri
"
,
"
Sat
"
];
var
months
=
[
"
Jan
"
,
"
Feb
"
,
"
Mar
"
,
"
Apr
"
,
"
May
"
,
"
Jun
"
,
"
Jul
"
,
"
Aug
"
,
"
Sep
"
,
"
Oct
"
,
"
Nov
"
,
"
Dec
"
];
...
...
@@ -37,7 +39,7 @@
return
((
value
<
10
)
?
'
0
'
:
''
)
+
value
;
};
var
dateToUTCString
=
function
(
date
)
{
Utils
.
dateToUTCString
=
function
(
date
)
{
var
time
=
valueToTwoDigits
(
date
.
getUTCHours
())
+
'
:
'
+
valueToTwoDigits
(
date
.
getUTCMinutes
())
+
'
:
'
+
valueToTwoDigits
(
date
.
getUTCSeconds
());
return
days
[
date
.
getUTCDay
()]
+
'
,
'
+
valueToTwoDigits
(
date
.
getUTCDate
())
+
'
'
+
months
[
date
.
getUTCMonth
()]
+
'
'
+
date
.
getUTCFullYear
()
+
'
'
+
time
+
'
GMT
'
;
};
...
...
@@ -64,7 +66,7 @@
return
value
.
replace
(
/^
\s
*/
,
""
).
replace
(
/
\s
*$/
,
""
);
};
var
parseJSON
=
function
(
data
)
{
Utils
.
parseJSON
=
function
(
data
)
{
if
(
!
data
||
!
isString
(
data
))
{
return
null
;
}
...
...
@@ -310,10 +312,10 @@
return
(
text
)
?
window
.
unescape
(
text
)
:
''
;
};
var
parseMessage
=
function
(
messageText
,
keys
)
{
Utils
.
parseMessage
=
function
(
messageText
,
keys
)
{
var
msg
=
messageText
;
if
(
isString
(
messageText
))
{
msg
=
parseJSON
(
messageText
);
msg
=
Utils
.
parseJSON
(
messageText
);
}
var
message
=
{
...
...
@@ -377,7 +379,7 @@
return
url
;
};
var
extract_xss_domain
=
function
(
domain
)
{
Utils
.
extract_xss_domain
=
function
(
domain
)
{
// if domain is an ip address return it, else return ate least the last two parts of it
if
(
domain
.
match
(
/^
(\d{1,3}\.){3}\d{1,3}
$/
))
{
return
domain
;
...
...
@@ -407,7 +409,7 @@
/* common callbacks */
var
onmessageCallback
=
function
(
event
)
{
Log4js
.
info
(
"
[
"
+
this
.
type
+
"
] message received
"
,
arguments
);
var
message
=
parseMessage
(
event
.
data
,
this
.
pushstream
);
var
message
=
Utils
.
parseMessage
(
event
.
data
,
this
.
pushstream
);
this
.
pushstream
.
_onmessage
(
message
.
data
,
message
.
id
,
message
.
channel
,
message
.
eventid
,
true
);
};
...
...
@@ -526,7 +528,7 @@
StreamWrapper
.
prototype
=
{
connect
:
function
()
{
this
.
_closeCurrentConnection
();
var
domain
=
extract_xss_domain
(
this
.
pushstream
.
host
);
var
domain
=
Utils
.
extract_xss_domain
(
this
.
pushstream
.
host
);
try
{
document
.
domain
=
domain
;
}
catch
(
e
)
{
...
...
@@ -657,8 +659,8 @@
this
.
_closeCurrentConnection
();
this
.
connectionEnabled
=
true
;
this
.
xhrSettings
.
url
=
getSubscriberUrl
(
this
.
pushstream
,
this
.
pushstream
.
urlPrefixLongpolling
);
var
domain
=
extract_xss_domain
(
this
.
pushstream
.
host
);
var
currentDomain
=
extract_xss_domain
(
window
.
location
.
hostname
);
var
domain
=
Utils
.
extract_xss_domain
(
this
.
pushstream
.
host
);
var
currentDomain
=
Utils
.
extract_xss_domain
(
window
.
location
.
hostname
);
var
port
=
this
.
pushstream
.
port
;
var
currentPort
=
window
.
location
.
port
||
(
this
.
pushstream
.
useSSL
?
443
:
80
);
this
.
useJSONP
=
(
domain
!==
currentDomain
)
||
(
port
!==
currentPort
)
||
this
.
pushstream
.
longPollingUseJSONP
;
...
...
@@ -711,7 +713,7 @@
if
(
this
.
lastModified
===
null
)
{
var
date
=
new
Date
();
if
(
this
.
pushstream
.
secondsAgo
)
{
date
.
setTime
(
date
.
getTime
()
-
(
this
.
pushstream
.
secondsAgo
*
1000
));
}
this
.
lastModified
=
dateToUTCString
(
date
);
this
.
lastModified
=
Utils
.
dateToUTCString
(
date
);
}
if
(
!
this
.
pushstream
.
longPollingByHeaders
)
{
...
...
@@ -757,7 +759,7 @@
var
messages
=
isArray
(
responseText
)
?
responseText
:
responseText
.
split
(
"
\r\n
"
);
for
(
var
i
=
0
;
i
<
messages
.
length
;
i
++
)
{
if
(
messages
[
i
])
{
lastMessage
=
parseMessage
(
messages
[
i
],
this
.
pushstream
);
lastMessage
=
Utils
.
parseMessage
(
messages
[
i
],
this
.
pushstream
);
this
.
messagesQueue
.
push
(
lastMessage
);
if
(
!
this
.
pushstream
.
longPollingByHeaders
&&
lastMessage
.
time
)
{
this
.
etag
=
lastMessage
.
tag
;
...
...
@@ -820,7 +822,7 @@
this
.
wrappers
=
[];
this
.
wrapper
=
null
;
this
.
on
open
=
null
;
this
.
on
channeldeleted
=
null
;
this
.
onmessage
=
null
;
this
.
onerror
=
null
;
this
.
onstatuschange
=
null
;
...
...
misc/spec/javascripts/PushStreamSpec.js
View file @
28d810db
...
...
@@ -19,6 +19,12 @@ describe("PushStream", function() {
it
(
"
should define status code constants
"
,
function
()
{
expect
(
PushStream
.
CLOSED
).
toBeDefined
();
expect
(
PushStream
.
CONNECTING
).
toBeDefined
();
expect
(
PushStream
.
OPEN
).
toBeDefined
();
});
it
(
"
should has a PushStream instances manager
"
,
function
()
{
expect
(
PushStreamManager
).
toBeDefined
();
expect
(
PushStreamManager
instanceof
Array
).
toBeTruthy
();
});
});
...
...
@@ -45,28 +51,92 @@ describe("PushStream", function() {
expect
(
pushstream
.
readyState
).
toBe
(
PushStream
.
CLOSED
);
});
it
(
"
should use '/sub' as url prefix for stream
"
,
function
()
{
describe
(
"
for operation timeouts
"
,
function
()
{
it
(
"
should has a connection timeout
"
,
function
()
{
expect
(
pushstream
.
timeout
).
toBe
(
15000
);
});
it
(
"
should has a ping message timeout
"
,
function
()
{
expect
(
pushstream
.
pingtimeout
).
toBe
(
30000
);
});
it
(
"
should has a reconnect interval, to be used when a timeout happens
"
,
function
()
{
expect
(
pushstream
.
reconnecttimeout
).
toBe
(
3000
);
});
it
(
"
should has a reconnect interval, to be used when a channel is unavailable
"
,
function
()
{
expect
(
pushstream
.
checkChannelAvailabilityInterval
).
toBe
(
60000
);
});
});
describe
(
"
for url prefix
"
,
function
()
{
it
(
"
should use '/pub' for publish message
"
,
function
()
{
expect
(
pushstream
.
urlPrefixPublisher
).
toBe
(
'
/pub
'
);
});
it
(
"
should use '/sub' for stream
"
,
function
()
{
expect
(
pushstream
.
urlPrefixStream
).
toBe
(
'
/sub
'
);
});
it
(
"
should use '/ev' as url prefix
for event source
"
,
function
()
{
it
(
"
should use '/ev'
for event source
"
,
function
()
{
expect
(
pushstream
.
urlPrefixEventsource
).
toBe
(
'
/ev
'
);
});
it
(
"
should use '/lp' as url prefix
for long-polling
"
,
function
()
{
it
(
"
should use '/lp'
for long-polling
"
,
function
()
{
expect
(
pushstream
.
urlPrefixLongpolling
).
toBe
(
'
/lp
'
);
});
it
(
"
should use '/ws' as url prefix
for websocket
"
,
function
()
{
it
(
"
should use '/ws'
for websocket
"
,
function
()
{
expect
(
pushstream
.
urlPrefixWebsocket
).
toBe
(
'
/ws
'
);
});
});
describe
(
"
for json keys
"
,
function
()
{
it
(
"
should has a key for 'id'
"
,
function
()
{
expect
(
pushstream
.
jsonIdKey
).
toBe
(
'
id
'
);
});
it
(
"
should has a key for 'channel'
"
,
function
()
{
expect
(
pushstream
.
jsonChannelKey
).
toBe
(
'
channel
'
);
});
it
(
"
should has a key for 'text'
"
,
function
()
{
expect
(
pushstream
.
jsonDataKey
).
toBe
(
'
text
'
);
});
it
(
"
should has a key for 'tag'
"
,
function
()
{
expect
(
pushstream
.
jsonTagKey
).
toBe
(
'
tag
'
);
});
it
(
"
should has a key for 'time'
"
,
function
()
{
expect
(
pushstream
.
jsonTimeKey
).
toBe
(
'
time
'
);
});
it
(
"
should has a key for 'eventid'
"
,
function
()
{
expect
(
pushstream
.
jsonEventIdKey
).
toBe
(
'
eventid
'
);
});
});
describe
(
"
for arguments names
"
,
function
()
{
it
(
"
should has a argument for 'tag'
"
,
function
()
{
expect
(
pushstream
.
longPollingTagArgument
).
toBe
(
'
tag
'
);
});
it
(
"
should has a argument for 'time'
"
,
function
()
{
expect
(
pushstream
.
longPollingTimeArgument
).
toBe
(
'
time
'
);
});
it
(
"
should has a argument for 'channels'
"
,
function
()
{
expect
(
pushstream
.
channelsArgument
).
toBe
(
'
channels
'
);
});
});
it
(
"
should has all modes availables
"
,
function
()
{
expect
(
pushstream
.
modes
).
toEqual
([
'
eventsource
'
,
'
websocket
'
,
'
stream
'
,
'
longpolling
'
]);
});
it
(
"
should define callbacks attributes
"
,
function
()
{
expect
(
pushstream
.
on
open
).
toBeDefined
();
expect
(
pushstream
.
on
channeldeleted
).
toBeDefined
();
expect
(
pushstream
.
onmessage
).
toBeDefined
();
expect
(
pushstream
.
onerror
).
toBeDefined
();
expect
(
pushstream
.
onstatuschange
).
toBeDefined
();
...
...
@@ -147,5 +217,4 @@ describe("PushStream", function() {
});
});
});
});
misc/spec/javascripts/
PushStream
UtilsSpec.js
→
misc/spec/javascripts/UtilsSpec.js
View file @
28d810db
describe
(
"
PushStream
Utils
"
,
function
()
{
describe
(
"
Utils
"
,
function
()
{
var
jsonKeys
=
{
jsonIdKey
:
'
id
'
,
jsonChannelKey
:
'
channel
'
,
...
...
@@ -13,62 +13,62 @@ describe("PushStreamUtils", function() {
describe
(
"
when formatting dates to UTC string
"
,
function
()
{
it
(
"
should return the string with two digits for day
"
,
function
()
{
expect
(
dateToUTCString
(
new
Date
(
"
2012-11-09T12:00:00-03:00
"
))).
toBe
(
"
Fri, 09 Nov 2012 15:00:00 GMT
"
);
expect
(
dateToUTCString
(
new
Date
(
"
2012-11-10T12:00:00-03:00
"
))).
toBe
(
"
Sat, 10 Nov 2012 15:00:00 GMT
"
);
expect
(
Utils
.
dateToUTCString
(
new
Date
(
"
2012-11-09T12:00:00-03:00
"
))).
toBe
(
"
Fri, 09 Nov 2012 15:00:00 GMT
"
);
expect
(
Utils
.
dateToUTCString
(
new
Date
(
"
2012-11-10T12:00:00-03:00
"
))).
toBe
(
"
Sat, 10 Nov 2012 15:00:00 GMT
"
);
});
it
(
"
should return the string with two digits for hour
"
,
function
()
{
expect
(
dateToUTCString
(
new
Date
(
"
2012-11-09T06:00:00-03:00
"
))).
toBe
(
"
Fri, 09 Nov 2012 09:00:00 GMT
"
);
expect
(
dateToUTCString
(
new
Date
(
"
2012-11-10T07:00:00-03:00
"
))).
toBe
(
"
Sat, 10 Nov 2012 10:00:00 GMT
"
);
expect
(
Utils
.
dateToUTCString
(
new
Date
(
"
2012-11-09T06:00:00-03:00
"
))).
toBe
(
"
Fri, 09 Nov 2012 09:00:00 GMT
"
);
expect
(
Utils
.
dateToUTCString
(
new
Date
(
"
2012-11-10T07:00:00-03:00
"
))).
toBe
(
"
Sat, 10 Nov 2012 10:00:00 GMT
"
);
});
it
(
"
should return the string with two digits for minute
"
,
function
()
{
expect
(
dateToUTCString
(
new
Date
(
"
2012-11-09T06:09:00-03:00
"
))).
toBe
(
"
Fri, 09 Nov 2012 09:09:00 GMT
"
);
expect
(
dateToUTCString
(
new
Date
(
"
2012-11-10T07:10:00-03:00
"
))).
toBe
(
"
Sat, 10 Nov 2012 10:10:00 GMT
"
);
expect
(
Utils
.
dateToUTCString
(
new
Date
(
"
2012-11-09T06:09:00-03:00
"
))).
toBe
(
"
Fri, 09 Nov 2012 09:09:00 GMT
"
);
expect
(
Utils
.
dateToUTCString
(
new
Date
(
"
2012-11-10T07:10:00-03:00
"
))).
toBe
(
"
Sat, 10 Nov 2012 10:10:00 GMT
"
);
});
it
(
"
should return the string with two digits for second
"
,
function
()
{
expect
(
dateToUTCString
(
new
Date
(
"
2012-11-09T06:09:09-03:00
"
))).
toBe
(
"
Fri, 09 Nov 2012 09:09:09 GMT
"
);
expect
(
dateToUTCString
(
new
Date
(
"
2012-11-10T07:10:10-03:00
"
))).
toBe
(
"
Sat, 10 Nov 2012 10:10:10 GMT
"
);
expect
(
Utils
.
dateToUTCString
(
new
Date
(
"
2012-11-09T06:09:09-03:00
"
))).
toBe
(
"
Fri, 09 Nov 2012 09:09:09 GMT
"
);
expect
(
Utils
.
dateToUTCString
(
new
Date
(
"
2012-11-10T07:10:10-03:00
"
))).
toBe
(
"
Sat, 10 Nov 2012 10:10:10 GMT
"
);
});
it
(
"
should return the right text for months
"
,
function
()
{
expect
(
dateToUTCString
(
new
Date
(
"
2012-01-09T06:09:09-03:00
"
))).
toBe
(
"
Mon, 09 Jan 2012 09:09:09 GMT
"
);
expect
(
dateToUTCString
(
new
Date
(
"
2012-02-09T06:09:09-03:00
"
))).
toBe
(
"
Thu, 09 Feb 2012 09:09:09 GMT
"
);
expect
(
dateToUTCString
(
new
Date
(
"
2012-03-09T06:09:09-03:00
"
))).
toBe
(
"
Fri, 09 Mar 2012 09:09:09 GMT
"
);
expect
(
dateToUTCString
(
new
Date
(
"
2012-04-09T06:09:09-03:00
"
))).
toBe
(
"
Mon, 09 Apr 2012 09:09:09 GMT
"
);
expect
(
dateToUTCString
(
new
Date
(
"
2012-05-09T06:09:09-03:00
"
))).
toBe
(
"
Wed, 09 May 2012 09:09:09 GMT
"
);
expect
(
dateToUTCString
(
new
Date
(
"
2012-06-09T06:09:09-03:00
"
))).
toBe
(
"
Sat, 09 Jun 2012 09:09:09 GMT
"
);
expect
(
dateToUTCString
(
new
Date
(
"
2012-07-09T06:09:09-03:00
"
))).
toBe
(
"
Mon, 09 Jul 2012 09:09:09 GMT
"
);
expect
(
dateToUTCString
(
new
Date
(
"
2012-08-09T06:09:09-03:00
"
))).
toBe
(
"
Thu, 09 Aug 2012 09:09:09 GMT
"
);
expect
(
dateToUTCString
(
new
Date
(
"
2012-09-09T06:09:09-03:00
"
))).
toBe
(
"
Sun, 09 Sep 2012 09:09:09 GMT
"
);
expect
(
dateToUTCString
(
new
Date
(
"
2012-10-09T06:09:09-03:00
"
))).
toBe
(
"
Tue, 09 Oct 2012 09:09:09 GMT
"
);
expect
(
dateToUTCString
(
new
Date
(
"
2012-11-09T06:09:09-03:00
"
))).
toBe
(
"
Fri, 09 Nov 2012 09:09:09 GMT
"
);
expect
(
dateToUTCString
(
new
Date
(
"
2012-12-09T06:09:09-03:00
"
))).
toBe
(
"
Sun, 09 Dec 2012 09:09:09 GMT
"
);
expect
(
Utils
.
dateToUTCString
(
new
Date
(
"
2012-01-09T06:09:09-03:00
"
))).
toBe
(
"
Mon, 09 Jan 2012 09:09:09 GMT
"
);
expect
(
Utils
.
dateToUTCString
(
new
Date
(
"
2012-02-09T06:09:09-03:00
"
))).
toBe
(
"
Thu, 09 Feb 2012 09:09:09 GMT
"
);
expect
(
Utils
.
dateToUTCString
(
new
Date
(
"
2012-03-09T06:09:09-03:00
"
))).
toBe
(
"
Fri, 09 Mar 2012 09:09:09 GMT
"
);
expect
(
Utils
.
dateToUTCString
(
new
Date
(
"
2012-04-09T06:09:09-03:00
"
))).
toBe
(
"
Mon, 09 Apr 2012 09:09:09 GMT
"
);
expect
(
Utils
.
dateToUTCString
(
new
Date
(
"
2012-05-09T06:09:09-03:00
"
))).
toBe
(
"
Wed, 09 May 2012 09:09:09 GMT
"
);
expect
(
Utils
.
dateToUTCString
(
new
Date
(
"
2012-06-09T06:09:09-03:00
"
))).
toBe
(
"
Sat, 09 Jun 2012 09:09:09 GMT
"
);
expect
(
Utils
.
dateToUTCString
(
new
Date
(
"
2012-07-09T06:09:09-03:00
"
))).
toBe
(
"
Mon, 09 Jul 2012 09:09:09 GMT
"
);
expect
(
Utils
.
dateToUTCString
(
new
Date
(
"
2012-08-09T06:09:09-03:00
"
))).
toBe
(
"
Thu, 09 Aug 2012 09:09:09 GMT
"
);
expect
(
Utils
.
dateToUTCString
(
new
Date
(
"
2012-09-09T06:09:09-03:00
"
))).
toBe
(
"
Sun, 09 Sep 2012 09:09:09 GMT
"
);
expect
(
Utils
.
dateToUTCString
(
new
Date
(
"
2012-10-09T06:09:09-03:00
"
))).
toBe
(
"
Tue, 09 Oct 2012 09:09:09 GMT
"
);
expect
(
Utils
.
dateToUTCString
(
new
Date
(
"
2012-11-09T06:09:09-03:00
"
))).
toBe
(
"
Fri, 09 Nov 2012 09:09:09 GMT
"
);
expect
(
Utils
.
dateToUTCString
(
new
Date
(
"
2012-12-09T06:09:09-03:00
"
))).
toBe
(
"
Sun, 09 Dec 2012 09:09:09 GMT
"
);
});
it
(
"
should return the right text for days
"
,
function
()
{
expect
(
dateToUTCString
(
new
Date
(
"
2012-01-01T06:09:09-03:00
"
))).
toBe
(
"
Sun, 01 Jan 2012 09:09:09 GMT
"
);
expect
(
dateToUTCString
(
new
Date
(
"
2012-01-02T06:09:09-03:00
"
))).
toBe
(
"
Mon, 02 Jan 2012 09:09:09 GMT
"
);
expect
(
dateToUTCString
(
new
Date
(
"
2012-01-03T06:09:09-03:00
"
))).
toBe
(
"
Tue, 03 Jan 2012 09:09:09 GMT
"
);
expect
(
dateToUTCString
(
new
Date
(
"
2012-01-04T06:09:09-03:00
"
))).
toBe
(
"
Wed, 04 Jan 2012 09:09:09 GMT
"
);
expect
(
dateToUTCString
(
new
Date
(
"
2012-01-05T06:09:09-03:00
"
))).
toBe
(
"
Thu, 05 Jan 2012 09:09:09 GMT
"
);
expect
(
dateToUTCString
(
new
Date
(
"
2012-01-06T06:09:09-03:00
"
))).
toBe
(
"
Fri, 06 Jan 2012 09:09:09 GMT
"
);
expect
(
dateToUTCString
(
new
Date
(
"
2012-01-07T06:09:09-03:00
"
))).
toBe
(
"
Sat, 07 Jan 2012 09:09:09 GMT
"
);
expect
(
Utils
.
dateToUTCString
(
new
Date
(
"
2012-01-01T06:09:09-03:00
"
))).
toBe
(
"
Sun, 01 Jan 2012 09:09:09 GMT
"
);
expect
(
Utils
.
dateToUTCString
(
new
Date
(
"
2012-01-02T06:09:09-03:00
"
))).
toBe
(
"
Mon, 02 Jan 2012 09:09:09 GMT
"
);
expect
(
Utils
.
dateToUTCString
(
new
Date
(
"
2012-01-03T06:09:09-03:00
"
))).
toBe
(
"
Tue, 03 Jan 2012 09:09:09 GMT
"
);
expect
(
Utils
.
dateToUTCString
(
new
Date
(
"
2012-01-04T06:09:09-03:00
"
))).
toBe
(
"
Wed, 04 Jan 2012 09:09:09 GMT
"
);
expect
(
Utils
.
dateToUTCString
(
new
Date
(
"
2012-01-05T06:09:09-03:00
"
))).
toBe
(
"
Thu, 05 Jan 2012 09:09:09 GMT
"
);
expect
(
Utils
.
dateToUTCString
(
new
Date
(
"
2012-01-06T06:09:09-03:00
"
))).
toBe
(
"
Fri, 06 Jan 2012 09:09:09 GMT
"
);
expect
(
Utils
.
dateToUTCString
(
new
Date
(
"
2012-01-07T06:09:09-03:00
"
))).
toBe
(
"
Sat, 07 Jan 2012 09:09:09 GMT
"
);
});
});
describe
(
"
when parse JSON
"
,
function
()
{
it
(
"
should return null when data is null
"
,
function
()
{
expect
(
parseJSON
(
null
)).
toBe
(
null
);
expect
(
Utils
.
parseJSON
(
null
)).
toBe
(
null
);
});
it
(
"
should return null when data is undefined
"
,
function
()
{
expect
(
parseJSON
(
undefined
)).
toBe
(
null
);
expect
(
Utils
.
parseJSON
(
undefined
)).
toBe
(
null
);
});
it
(
"
should return null when data is not a string
"
,
function
()
{
expect
(
parseJSON
({})).
toBe
(
null
);
expect
(
Utils
.
parseJSON
({})).
toBe
(
null
);
});
if
(
window
.
JSON
)
{
...
...
@@ -85,24 +85,24 @@ describe("PushStreamUtils", function() {
it
(
"
should use the browser default implementation when available
"
,
function
()
{
spyOn
(
window
.
JSON
,
"
parse
"
);
parseJSON
(
'
{"a":1}
'
)
Utils
.
parseJSON
(
'
{"a":1}
'
)
expect
(
window
.
JSON
.
parse
).
toHaveBeenCalledWith
(
'
{"a":1}
'
);
});
it
(
"
should parse a well formed json string
"
,
function
()
{
expect
(
parseJSON
(
'
{"a":1}
'
)[
"
a
"
]).
toBe
(
1
);
expect
(
Utils
.
parseJSON
(
'
{"a":1}
'
)[
"
a
"
]).
toBe
(
1
);
});
it
(
"
should parse when the string has leading spaces
"
,
function
()
{
expect
(
parseJSON
(
'
{"a":1}
'
)[
"
a
"
]).
toBe
(
1
);
expect
(
Utils
.
parseJSON
(
'
{"a":1}
'
)[
"
a
"
]).
toBe
(
1
);
});
it
(
"
should parse when the string has trailing spaces
"
,
function
()
{
expect
(
parseJSON
(
'
{"a":1}
'
)[
"
a
"
]).
toBe
(
1
);
expect
(
Utils
.
parseJSON
(
'
{"a":1}
'
)[
"
a
"
]).
toBe
(
1
);
});
it
(
"
should raise error when string is a invalid json
"
,
function
()
{
expect
(
function
()
{
parseJSON
(
'
{"a":1[]}
'
)
}).
toThrow
(
'
Invalid JSON: {"a":1[]}
'
);
expect
(
function
()
{
Utils
.
parseJSON
(
'
{"a":1[]}
'
)
}).
toThrow
(
'
Invalid JSON: {"a":1[]}
'
);
});
});
}
...
...
@@ -119,49 +119,49 @@ describe("PushStreamUtils", function() {
});
it
(
"
should parse a well formed json string
"
,
function
()
{
expect
(
parseJSON
(
'
{"a":1}
'
)[
"
a
"
]).
toBe
(
1
);
expect
(
Utils
.
parseJSON
(
'
{"a":1}
'
)[
"
a
"
]).
toBe
(
1
);
});
it
(
"
should parse when the string has leading spaces
"
,
function
()
{
expect
(
parseJSON
(
'
{"a":1}
'
)[
"
a
"
]).
toBe
(
1
);
expect
(
Utils
.
parseJSON
(
'
{"a":1}
'
)[
"
a
"
]).
toBe
(
1
);
});
it
(
"
should parse when the string has trailing spaces
"
,
function
()
{
expect
(
parseJSON
(
'
{"a":1}
'
)[
"
a
"
]).
toBe
(
1
);
expect
(
Utils
.
parseJSON
(
'
{"a":1}
'
)[
"
a
"
]).
toBe
(
1
);
});
it
(
"
should raise error when string is a invalid json
"
,
function
()
{
expect
(
function
()
{
parseJSON
(
'
{"a":1[]}
'
)
}).
toThrow
(
'
Invalid JSON: {"a":1[]}
'
);
expect
(
function
()
{
Utils
.
parseJSON
(
'
{"a":1[]}
'
)
}).
toThrow
(
'
Invalid JSON: {"a":1[]}
'
);
});
});
});
describe
(
"
when extract xss domain
"
,
function
()
{
it
(
"
should return the ip address when domain is only an ip
"
,
function
()
{
expect
(
extract_xss_domain
(
"
201.10.32.52
"
)).
toBe
(
"
201.10.32.52
"
);
expect
(
Utils
.
extract_xss_domain
(
"
201.10.32.52
"
)).
toBe
(
"
201.10.32.52
"
);
});
it
(
"
should return the full domain when it has only two parts
"
,
function
()
{
expect
(
extract_xss_domain
(
"
domain.com
"
)).
toBe
(
"
domain.com
"
);
expect
(
Utils
.
extract_xss_domain
(
"
domain.com
"
)).
toBe
(
"
domain.com
"
);
});
it
(
"
should return the last two parts when domain has three parts
"
,
function
()
{
expect
(
extract_xss_domain
(
"
example.domain.com
"
)).
toBe
(
"
domain.com
"
);
expect
(
Utils
.
extract_xss_domain
(
"
example.domain.com
"
)).
toBe
(
"
domain.com
"
);
});
it
(
"
should return all parts minus the first one when domain has more than three parts
"
,
function
()
{
expect
(
extract_xss_domain
(
"
another.example.domain.com
"
)).
toBe
(
"
example.domain.com
"
);
expect
(
Utils
.
extract_xss_domain
(
"
another.example.domain.com
"
)).
toBe
(
"
example.domain.com
"
);
});
});
describe
(
"
when parsing a message
"
,
function
()
{
it
(
"
should accept a simple string as text
"
,
function
()
{
var
message
=
parseMessage
(
'
{"id":31,"channel":"54x19","text":"some simple string"}
'
,
jsonKeys
);
var
message
=
Utils
.
parseMessage
(
'
{"id":31,"channel":"54x19","text":"some simple string"}
'
,
jsonKeys
);
expect
(
message
.
data
).
toBe
(
"
some simple string
"
);
});
it
(
"
should accept a json as text
"
,
function
()
{
var
message
=
parseMessage
(
'
{"id":31,"channel":"54x19","text":{"id":"500516b7639e4029b8000001","type":"Player","change":{"loc":[54.34772390000001,18.5610535],"version":7}}}
'
,
jsonKeys
);
var
message
=
Utils
.
parseMessage
(
'
{"id":31,"channel":"54x19","text":{"id":"500516b7639e4029b8000001","type":"Player","change":{"loc":[54.34772390000001,18.5610535],"version":7}}}
'
,
jsonKeys
);
expect
(
message
.
data
.
id
).
toBe
(
"
500516b7639e4029b8000001
"
);
expect
(
message
.
data
.
type
).
toBe
(
"
Player
"
);
expect
(
message
.
data
.
change
.
loc
[
0
]).
toBe
(
54.34772390000001
);
...
...
@@ -170,7 +170,7 @@ describe("PushStreamUtils", function() {
});
it
(
"
should accept an escaped json as text
"
,
function
()
{
var
message
=
parseMessage
(
'
{"id":31,"channel":"54x19","text":"%7B%22id%22%3A%22500516b7639e4029b8000001%22%2C%22type%22%3A%22Player%22%2C%22change%22%3A%7B%22loc%22%3A%5B54.34772390000001%2C18.5610535%5D%2C%22version%22%3A7%7D%7D"}
'
,
jsonKeys
);
var
message
=
Utils
.
parseMessage
(
'
{"id":31,"channel":"54x19","text":"%7B%22id%22%3A%22500516b7639e4029b8000001%22%2C%22type%22%3A%22Player%22%2C%22change%22%3A%7B%22loc%22%3A%5B54.34772390000001%2C18.5610535%5D%2C%22version%22%3A7%7D%7D"}
'
,
jsonKeys
);
expect
(
message
.
data
).
toBe
(
'
{"id":"500516b7639e4029b8000001","type":"Player","change":{"loc":[54.34772390000001,18.5610535],"version":7}}
'
);
});
});
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment