Skip to content

Commit

Permalink
Merge pull request #4 from songz/master
Browse files Browse the repository at this point in the history
Wrote test cases, and many updates
  • Loading branch information
bsstoner committed May 18, 2012
2 parents e6e6125 + fd5028a commit 7670274
Show file tree
Hide file tree
Showing 3 changed files with 159 additions and 18 deletions.
3 changes: 3 additions & 0 deletions README.md
Expand Up @@ -48,3 +48,6 @@ Python/Ruby versions.
OpenTok Dev Mailing List:
http://groups.google.com/group/opentok-developers

## Want to contribute?
### To run test suite:
jasmine-node --coffee --autotest spec/
55 changes: 37 additions & 18 deletions lib/opentok.js
Expand Up @@ -5,7 +5,8 @@ var https = require('https')
// tokbox constants:
, TOKEN_SENTINEL = "T1=="
, SDK_VERSION = "tbjs-0.91.2011-03-16"
, TOKBOX_HOST = "staging.tokbox.com"
, STAGING_HOST = "staging.tokbox.com"
, PROD_HOST = "api.opentok.com"
, SESSION_API_ENDPOINT = "/hl/session/create";

// Session Properties definition:
Expand Down Expand Up @@ -33,20 +34,34 @@ var OpenTokSession = function(sessionId){
}

// The SDK
var OpenTokSDK = exports.OpenTokSDK = function(partnerId, partnerSecret){
var OpenTokSDK = exports.OpenTokSDK = function(partnerId, partnerSecret, properties){
this.partnerId = partnerId
this.partnerSecret = partnerSecret
this.api_url = STAGING_HOST;

if(properties && properties['API_URL']){
if(properties['API_URL'].split('staging').length > 1){
// This is meant to guart against http://staging.tokbox.com vs staging.tokbox.com vs https...
this.api_url = STAGING_HOST;
}else{
this.api_url = PROD_HOST;
}
}
}

OpenTokSDK.prototype.setEnvironment = function(host){
TOKBOX_HOST = host
if(host.split('staging').length >1 ){
this.api_url = STAGING_HOST
}else{
this.api_url = PROD_HOST
}
}

OpenTokSDK.prototype.generateToken = function(ops){
ops = ops || {}

// grab options or use defaults:
var sessionId = (ops.session) ? ops.session.sessionId : this.sessionId;
var sessionId = (ops.session_id) ? ops.session_id : this.sessionId;

var createTime = OpenTokSDK.prototype._getUTCDate()
, sig
Expand Down Expand Up @@ -77,6 +92,10 @@ OpenTokSDK.prototype.generateToken = function(ops){
}

OpenTokSDK.prototype.createSession = function(ipPassthru, properties, callback){
// Since properties is optional, we need to check the number of parameters passed in
if(!callback){
callback = properties
}
var sessionId
, params = {
partner_id: this.partnerId,
Expand All @@ -87,9 +106,17 @@ OpenTokSDK.prototype.createSession = function(ipPassthru, properties, callback){
params[p] = properties[p]
}

sessionId = this._doRequest(params, function(sessionId){
callback(new OpenTokSession(sessionId))
})
var self = this;
sessionId = this._doRequest(params, function(chunks){
var start = chunks.match('<session_id>')
, end = chunks.match('</session_id>')
, sessionId;

if(start && end){
self.sessionId = chunks.substr(start.index + 12, (end.index - start.index - 12))
}
callback(self.sessionId)
});

}

Expand All @@ -103,11 +130,11 @@ OpenTokSDK.prototype._doRequest = function(params, callback){
var dataString = querystring.stringify(params);

options = {
host: TOKBOX_HOST,
host: this.api_url,
path: SESSION_API_ENDPOINT,
method: 'POST',
headers: {
'Content-Type': 'application-xml',
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': dataString.length,
'X-TB-PARTNER-AUTH': this.partnerId + ":" + this.partnerSecret
}
Expand All @@ -123,15 +150,7 @@ OpenTokSDK.prototype._doRequest = function(params, callback){
})

res.on('end', function(){
var start = chunks.match('<session_id>')
, end = chunks.match('</session_id>');

if(start && end){
var session = chunks.substr(start.index + 12, (end.index - start.index - 12))
callback(session)
} else {
callback()
}
callback(chunks);
})
})
req.write(dataString)
Expand Down
119 changes: 119 additions & 0 deletions spec/opentok_spec.coffee
@@ -0,0 +1,119 @@
OpenTok = require('../lib/opentok')

describe "Staging Environment", ->
stagingKey = '14971292'
stagingSecret = 'ecbe2b25afec7887bd72fe4763b87add8ce02658'
opentok = new OpenTok.OpenTokSDK(stagingKey, stagingSecret, {API_URL:'staging.tokbox.com'})

it "should pass in apikey, secret, and api_url", ->
expect(opentok.partnerId).toEqual(stagingKey)
expect(opentok.partnerSecret).toEqual(stagingSecret)
expect(opentok.api_url).toEqual('staging.tokbox.com')

it "should auto set api_url defaults", ->
opentok = new OpenTok.OpenTokSDK(stagingKey, stagingSecret)
expect(opentok.partnerId).toEqual(stagingKey)
expect(opentok.partnerSecret).toEqual(stagingSecret)
expect(opentok.api_url).toEqual('staging.tokbox.com')

it "should create session", ->
sessionId = null
queryFinished = false

waitsFor ->
return queryFinished
runs ->
expect(sessionId).not.toBeNull()
expect(sessionId.length).toBeGreaterThan(5)

opentok.createSession 'localhost', (result) ->
sessionId = result
queryFinished = true

it "should be able to set p2p to enabled", ->
result = null
queryFinished = false

waitsFor ->
return queryFinished
runs ->
expect(result).not.toBeNull()

opentok._doRequest {'p2p.preference':'enabled'}, (chunks) ->
start = chunks.match('<p2p>')
end = chunks.match('</p2p>')
p2p = chunks.substr(start.index + 5, (end.index - start.index - 5))
result = p2p.match('enabled')
queryFinished = true


it "should create session with p2p enabled", ->
sessionId = null
queryFinished = false

waitsFor ->
return queryFinished
runs ->
expect(sessionId).not.toBeNull()
expect(sessionId.length).toBeGreaterThan(5)

opentok.createSession 'localhost', {'p2p.preference':'enabled'}, (result) ->
sessionId = result
queryFinished = true

describe "Generating Tokens", ->
sessionId = '1_MX4xNDk3MTI5Mn5-MjAxMi0wNS0xNiAyMzoyMjozNC44NzQ0ODcrMDA6MDB-MC41MDI4NTI2OTA1MzR-'

it "should be backwards compatible with previous version of NodeJS module", ->
token = opentok.generateToken({session_id:undefined})
expect(token).not.toBeNull()
expect(token.length).toBeGreaterThan(5)

token = opentok.generateToken({sessionId:undefined})
expect(token).not.toBeNull()
expect(token.length).toBeGreaterThan(5)

it "should generate a valid input given sessionId", ->
token = opentok.generateToken({sessionId:sessionId})
expect(token).not.toBeNull()
expect(token.length).toBeGreaterThan(5)

describe "Production Environment", ->
stagingKey = '11421872'
stagingSecret = '296cebc2fc4104cd348016667ffa2a3909ec636f'
opentok = new OpenTok.OpenTokSDK(stagingKey, stagingSecret, {API_URL:'https://api.opentok.com/hl'})

it "should pass in apikey, secret, and api_url", ->
expect(opentok.partnerId).toEqual(stagingKey)
expect(opentok.partnerSecret).toEqual(stagingSecret)
expect(opentok.api_url).toEqual('api.opentok.com')

it "should create session", ->
sessionId = null
queryFinished = false

waitsFor ->
return queryFinished
runs ->
expect(sessionId).not.toBeNull()
expect(sessionId.length).toBeGreaterThan(5)

opentok.createSession 'localhost', (result) ->
sessionId = result
queryFinished = true

it "should be able to set p2p to enabled", ->
result = null
queryFinished = false

waitsFor ->
return queryFinished
runs ->
expect(result).not.toBeNull()

opentok._doRequest {'p2p.preference':'enabled'}, (chunks) ->
start = chunks.match('<p2p>')
end = chunks.match('</p2p>')
p2p = chunks.substr(start.index + 5, (end.index - start.index - 5))
result = p2p.match('enabled')
queryFinished = true

0 comments on commit 7670274

Please sign in to comment.