Skip to content
This repository has been archived by the owner on Jan 7, 2020. It is now read-only.

Commit

Permalink
fix/test_cases: update test cases for webfetch
Browse files Browse the repository at this point in the history
Updated test cases for webfetch issue
  • Loading branch information
Shankar committed Dec 1, 2017
1 parent 288286d commit bcc37b9
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 35 deletions.
67 changes: 38 additions & 29 deletions src/app.js
Expand Up @@ -139,48 +139,51 @@ class SAFEApp extends EventEmitter {
const tokens = path.split('/');

if (!tokens[tokens.length - 1] && tokens.length > 1) {
tokens.pop();
tokens.pop();
}

path = tokens.join('/') || `/${consts.INDEX_HTML}`;

const PUBLIC_NAME_NOT_FOUND = -103;
const SERVICE_NOT_FOUND = -106;
// const NFS_NOT_FOUND = -301;
// lets' unpack
const hostParts = hostname.split('.');
const lookupName = hostParts.pop(); // last one is 'domain'
const serviceName = hostParts.join('.') || 'www'; // all others are 'service'

return new Promise(async (resolve, reject) => {
const getServiceInfo = async (lookupName, serviceName) => {
const getServiceInfo = async (pubName, servName) => {
try {
const address = await this.crypto.sha3Hash(lookupName);
const address = await this.crypto.sha3Hash(pubName);
const servicesContainer = await this.mutableData.newPublic(address, consts.TAG_TYPE_DNS);
return await servicesContainer.get(serviceName);
} catch(err) {
if (err.code === -103) {
let error = new Error();
return await servicesContainer.get(servName);
} catch (err) {
if (err.code === PUBLIC_NAME_NOT_FOUND || err.code === SERVICE_NOT_FOUND) {
const error = new Error();
error.code = err.code;
error.message = 'Requested Service or Public Name is invalid.';
error.message = `Requested ${err.code === PUBLIC_NAME_NOT_FOUND ? 'public name' : 'service'} is not found`;
throw error;
}
throw err;
}
};

const getFile = async (emulation, path, shouldThrow) => {
try {
console.log('Fetching', path);
let file = await emulation.fetch(path);
return;
} catch(e) {
console.log(e.code, -301)
if(e.code !== -301) {
throw e;
}
if (shouldThrow) {
throw e;
}
}
};
// const getFile = async (emulation, path, shouldThrow) => {
// try {
// console.log('Fetching', path);
// let file = await emulation.fetch(path);
// return;
// } catch (e) {
// console.log(e.code, -301)
// if (e.code !== -301) {
// throw e;
// }
// if (shouldThrow) {
// throw e;
// }
// }
// };

const handleNfsFetchException = (error) => {
if (error.code !== -301) {
Expand All @@ -190,30 +193,36 @@ class SAFEApp extends EventEmitter {

try {
const serviceInfo = await getServiceInfo(lookupName, serviceName);
if (serviceInfo.buf.length === 0) {
const error = new Error();
error.code = SERVICE_NOT_FOUND;
error.message = 'Service not found';
return reject(error);
}
let serviceMd;
try {
serviceMd = await this.mutableData.fromSerial(serviceInfo.buf);
} catch(e) {
} catch (e) {
serviceMd = await this.mutableData.newPublic(serviceInfo.buf, consts.TAG_TYPE_WWW);
}
const emulation = await serviceMd.emulateAs('NFS');
let file;
try {
file = await emulation.fetch(path);
} catch (e) {
handleNfsFetchException(e)
handleNfsFetchException(e);
}
if (!file && path.startsWith('/')) {
try {
file = await emulation.fetch(path.replace('/', ''));
} catch(e) {
} catch (e) {
handleNfsFetchException(e);
}
}
if (!file && path.split('/').length > 1) {
try {
file = await emulation.fetch(`${path}/${consts.INDEX_HTML}`);
} catch(e) {
} catch (e) {
handleNfsFetchException(e);
}
}
Expand All @@ -222,9 +231,9 @@ class SAFEApp extends EventEmitter {
}
const openedFile = await emulation.open(file, consts.pubConsts.NFS_FILE_MODE_READ);
const data = await openedFile.read(
consts.pubConsts.NFS_FILE_START, consts.pubConsts.NFS_FILE_END);
consts.pubConsts.NFS_FILE_START, consts.pubConsts.NFS_FILE_END);
resolve(data);
} catch(e) {
} catch (e) {
reject(e);
}
});
Expand Down
11 changes: 5 additions & 6 deletions test/browsing.js
Expand Up @@ -14,13 +14,13 @@ const createRandomDomain = (content, path, service, authedApp) => {
const nfs = serviceMdata.emulateAs('NFS');
// let's write the file
return nfs.create(content)
.then((file) => nfs.insert(path || '', file))
.then((file) => nfs.insert(path || '/index.html', file))
.then(() => app.crypto.sha3Hash(domain)
.then((dnsName) => app.mutableData.newPublic(dnsName, consts.TAG_TYPE_DNS)
.then((dnsData) => serviceMdata.getNameAndTag()
.then((res) => {
const payload = {};
payload[service || ''] = res.name;
payload[service || 'www'] = res.name;
return dnsData.quickSetup(payload);
}))));
}))
Expand Down Expand Up @@ -252,8 +252,7 @@ describe('Browsing', () => {

it('should not find dns', () =>
client.webFetch('safe://domain_doesnt_exist')
.should.be.rejectedWith('Core error: Routing client error -> Requested data not found')
.then((err) => should(err.code).be.equal(-103))
.should.be.rejectedWith('Requested public name is not found')
);

it('should be case sensitive', () =>
Expand All @@ -264,7 +263,7 @@ describe('Browsing', () => {

it('should not find service', () =>
client.webFetch(`safe://faulty_service.${domain}`)
.should.be.rejectedWith('Service not found')
.should.be.rejectedWith('Requested service is not found')
);

it('should not find file', () =>
Expand All @@ -281,7 +280,7 @@ describe('Browsing', () => {

it('wrong path', () => createRandomDomain(content, '/my.file', '')
.then((newdomain) => createAnonTestApp()
.then((app) => app.webFetch(`safe://${newdomain}/my.file/`)
.then((app) => app.webFetch(`safe://${newdomain}/my.file/some`)
.should.be.rejectedWith('NFS error: File not found')
))
).timeout(20000);
Expand Down

0 comments on commit bcc37b9

Please sign in to comment.