ndn-js
Version:
A JavaScript client library for Named Data Networking
133 lines (116 loc) • 5.43 kB
HTML
<!--
* Copyright (C) 2014-2019 Regents of the University of California.
* @author: Jeff Thompson <jefft0@remap.ucla.edu>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
* A copy of the GNU Lesser General Public License is in the file COPYING.
-->
<html xmlns = "http://www.w3.org/1999/xhtml">
<head>
<title>NDN Name</title>
<script type="text/javascript" src="../../build/ndn.js"></script>
<script type="text/javascript">
function testName() {
var result = document.getElementById('result');
result.innerHTML = "";
var comp1 = new Buffer(['.'.charCodeAt(0)]);
var comp2 = new Buffer([0x00, 0x01, 0x02, 0x03]);
// \u00E9 is e with accent.
var entree = "entr\u00E9e";
// When Name constructor is passed an array, each item is a name component represented with Buffer,
// typed array, or string (parsed as UTF-8)
var name1 = new Name([entree, comp1, comp2]);
var name1Uri = name1.toUri();
var name1UriExpected = "/entr%C3%A9e/..../%00%01%02%03";
result.innerHTML += "Name from '" + entree + "', Buffer of '.' and Buffer of 0,1,2,3:<br/>";
if (name1Uri == name1UriExpected)
result.innerHTML += "SUCCESS: " + name1Uri + ".<br/>";
else
result.innerHTML += "ERROR: got " + name1Uri + ", expected " + name1UriExpected + " .<br/>";
result.innerHTML += "Compare with same Name from '" + entree + "', '.' and Buffer:<br/>";
// Equivalent with name1
var name2 = new Name([entree, ".", comp2]);
if (name2.size() != name1.size())
result.innerHTML += "ERROR: Got name with " + name2.size() +
" components, expected " + name1.size() + ".<br/>";
else {
var allEqual = true;
for (var i = 0; i < name1.size(); ++i) {
if (!name1.get(i).equals(name2.get(i))) {
allEqual = false;
result.innerHTML += "ERROR: Compare with " + name2.toUri() + ": Names differ at component at index " + i + ".<br/>";
}
}
if (allEqual)
result.innerHTML += "SUCCESS: Names are equal.<br/>";
}
result.innerHTML += "Compare with same Name from URI:<br/>";
// Equivalent with name1; when Name constructor is passed a string, it is treated as a URI
var name3 = new Name("/entr%C3%A9e/..../%00%01%02%03");
if (name3.size() != name1.size())
result.innerHTML += "ERROR: Got name with " + name3.size() +
" components, expected " + name1.size() + ".<br/>";
else {
var allEqual = true;
for (var i = 0; i < name1.size(); ++i) {
if (!name1.get(i).equals(name3.get(i))) {
allEqual = false;
result.innerHTML += "ERROR: Names differ at component at index " + i + ".<br/>";
}
}
if (allEqual)
result.innerHTML += "SUCCESS: Names are equal.<br/>";
}
result.innerHTML += "Compare with Name prefix of first 2 components:<br/>";
// Returns new Name([entree, "."])
var name4 = name2.getPrefix(2);
if (name4.size() != 2)
result.innerHTML += "ERROR: Got name with " + name4.size() +
" components, expected 2.<br/>";
else {
var allEqual = true;
for (var i = 0; i < name4.size(); ++i) {
if (!name1.get(i).equals(name4.get(i))) {
allEqual = false;
result.innerHTML += "ERROR: Names differ at component at index " + i + ".<br/>";
}
}
if (allEqual)
result.innerHTML += "SUCCESS: First 2 components are equal: " + name4.toUri() + " .<br/>";
}
result.innerHTML += "Check with Name component at index 2:<br/>";
// Returns ArrayBuffer of "%00%01%02%03"
var component2Out = name1.get(2).getValue().buf();
if (DataUtils.arraysEqual(component2Out, comp2))
result.innerHTML += "SUCCESS: Components at index 2 are equal.<br/>";
else
result.innerHTML += "ERROR: Components at index 2 are different.<br/>";
var name5 = new Name("/localhost/user/folders/files/%00%0F");
result.innerHTML += "Check chaining calls to append() to create Name: " + name5.toUri() + "<br/>";
if (name5.equals(new Name(new Name("/localhost")).append(new Name("/user/folders")).append
("files").appendSegment(15)))
result.innerHTML += "SUCCESS: Names are equal.<br/>";
else
result.innerHTML += "ERROR: Names are different.<br/>";
}
</script>
</head>
<body >
<button onclick="testName()">Test</button>
<p id="result"></p>
</body>
</html>