Skip to content

Instantly share code, notes, and snippets.

@rjswenson
Created November 18, 2013 20:48
Show Gist options
  • Save rjswenson/7535024 to your computer and use it in GitHub Desktop.
Save rjswenson/7535024 to your computer and use it in GitHub Desktop.
Jasmine Javascript testing for encoder
var encoder = {
setMessage: function() {
this.message = document.getElementById('message').value;
},
getPassword: function() {
this.password = prompt("Enter your Decoder Password: ");
},
setUrl: function() {
totalURL = document.URL + "?" +
encodeURI(this.encrypt());
},
replaceMessageWithUrl: function () {
replaceURL = totalURL.replace("encode.html", "decode.html");
document.getElementById('message').value = replaceURL;
},
encrypt: function() {
cypher = CryptoJS.AES.encrypt(this.message, this.password);
this.verify = CryptoJS.AES.decrypt(cypher, this.password).toString(CryptoJS.enc.Utf8);
return cypher
},
protect: function() {
this.setMessage();
this.getPassword();
this.setUrl();
this.replaceMessageWithUrl();
}
}
document.getElementById('encrypt').onclick = function() { encoder.protect() }
describe("Encoder", function(){
it("encodes a string into a link", function(){
encoder.protect = function(){
encoder.message = "A secret";
encoder.password = "password";
encoder.encrypt();
};
encoder.encrypt = function() {
cypher = CryptoJS.AES.encrypt(this.message, this.password);
this.verify = CryptoJS.AES.decrypt(cypher, this.password).toString(CryptoJS.enc.Utf8);
return cypher
};
encoder.protect();
expect(encoder.verify).toEqual("A secret");
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment