Accept Buffer on UserAgent.encode

This commit is contained in:
Kagami Hiiragi 2015-02-10 17:39:01 +03:00
parent 68c7966bef
commit 5ada61cbe1
2 changed files with 16 additions and 5 deletions

View File

@ -62,8 +62,8 @@ exports.parse = function(str) {
/** /**
* Encode user agent into `var_str` Buffer. Most underlying software * Encode user agent into `var_str` Buffer. Most underlying software
* comes first. * comes first.
* @param {(Object[]|string[]|string)} software - List of software to * @param {(Object[]|string[]|string|Buffer)} software - List of
* encode or just raw user agent string * software to encode or just raw user agent string/Buffer
* @return {Buffer} Encoded user agent. * @return {Buffer} Encoded user agent.
* @function * @function
* @static * @static
@ -83,6 +83,8 @@ var encode = exports.encode = function(software) {
return str; return str;
}).join("/"); }).join("/");
ua = "/" + ua + "/"; ua = "/" + ua + "/";
} else if (Buffer.isBuffer(software)) {
return software;
} else { } else {
ua = software; ua = software;
} }
@ -90,7 +92,7 @@ var encode = exports.encode = function(software) {
}; };
/** /**
* Encode bitmessage's user agent. * Encode bitmessage's library user agent.
* @return {Buffer} Encoded user agent. * @return {Buffer} Encoded user agent.
*/ */
exports.encodeSelf = function() { exports.encodeSelf = function() {
@ -98,8 +100,8 @@ exports.encodeSelf = function() {
}; };
/** /**
* Encode user agent with bitmessage's user agent underneath. Most * Encode user agent with bitmessage's library user agent underneath.
* underlying software comes first. * Most underlying software comes first.
* @param {(Object[]|string[]|Object|string)} software - List of * @param {(Object[]|string[]|Object|string)} software - List of
* software to encode * software to encode
* @return {Buffer} Encoded user agent. * @return {Buffer} Encoded user agent.

View File

@ -1327,5 +1327,14 @@ describe("High-level classes", function() {
expect(UserAgent.parse("/test").length).to.equal(0); expect(UserAgent.parse("/test").length).to.equal(0);
expect(UserAgent.parse("test/").length).to.equal(0); expect(UserAgent.parse("test/").length).to.equal(0);
}); });
it("should accept raw Buffer on encode", function() {
var enc = UserAgent.encodeSelfWith("test:0.0.1");
enc = UserAgent.encode(enc);
var software = UserAgent.parse(UserAgent.decode(enc).str);
expect(software[0].name).to.equal("bitmessage");
expect(software[1]).to.deep.equal({name: "test", version: "0.0.1"});
});
}); });
}); });