Some refactoring
This commit is contained in:
parent
94398b9d45
commit
79098f0c93
3 changed files with 282 additions and 232 deletions
155
test/basic.js
155
test/basic.js
|
@ -1,28 +1,28 @@
|
|||
var validWebResponse = {
|
||||
"d": {
|
||||
"results": [{
|
||||
"__metadata": {
|
||||
"uri": "https://api.datamarket.azure.com/Data.ashx/Bing/Search/Web?Query='xbox'&$skip=0&$top=1",
|
||||
"type": "WebResult"
|
||||
},
|
||||
"ID": "26888c2a-d245-47dc-87de-dc3551249de7",
|
||||
"Title": "Xbox | Games and Entertainment on All Your Devices",
|
||||
"Description": "Experience the new generation of games and entertainment with Xbox. Play Xbox games and stream video on all your devices.",
|
||||
"DisplayUrl": "www.xbox.com",
|
||||
"Url": "http://www.xbox.com/"
|
||||
}, {
|
||||
"__metadata": {
|
||||
"uri": "https://api.datamarket.azure.com/Data.ashx/Bing/Search/Web?Query='xbox'&$skip=1&$top=1",
|
||||
"type": "WebResult"
|
||||
},
|
||||
"ID": "ff23e110-31c2-44f9-be21-f213bcd4c654",
|
||||
"Title": "Amazon.com: Xbox - More Systems: Video Games: Games ...",
|
||||
"Description": "Online shopping for Video Games from a great selection of Games, Hardware, Computer And Console Video Game Products & more at everyday low prices.",
|
||||
"DisplayUrl": "www.amazon.com/Xbox-Games/b?ie=UTF8&node=537504",
|
||||
"Url": "http://www.amazon.com/Xbox-Games/b?ie=UTF8&node=537504"
|
||||
}],
|
||||
"__next": "https://api.datamarket.azure.com/Data.ashx/Bing/Search/Web?Query='xbox'&$skip=2"
|
||||
}
|
||||
"d": {
|
||||
"results": [{
|
||||
"__metadata": {
|
||||
"uri": "https://api.datamarket.azure.com/Data.ashx/Bing/Search/Web?Query='xbox'&$skip=0&$top=1",
|
||||
"type": "WebResult"
|
||||
},
|
||||
"ID": "26888c2a-d245-47dc-87de-dc3551249de7",
|
||||
"Title": "Xbox | Games and Entertainment on All Your Devices",
|
||||
"Description": "Experience the new generation of games and entertainment with Xbox. Play Xbox games and stream video on all your devices.",
|
||||
"DisplayUrl": "www.xbox.com",
|
||||
"Url": "http://www.xbox.com/"
|
||||
}, {
|
||||
"__metadata": {
|
||||
"uri": "https://api.datamarket.azure.com/Data.ashx/Bing/Search/Web?Query='xbox'&$skip=1&$top=1",
|
||||
"type": "WebResult"
|
||||
},
|
||||
"ID": "ff23e110-31c2-44f9-be21-f213bcd4c654",
|
||||
"Title": "Amazon.com: Xbox - More Systems: Video Games: Games ...",
|
||||
"Description": "Online shopping for Video Games from a great selection of Games, Hardware, Computer And Console Video Game Products & more at everyday low prices.",
|
||||
"DisplayUrl": "www.amazon.com/Xbox-Games/b?ie=UTF8&node=537504",
|
||||
"Url": "http://www.amazon.com/Xbox-Games/b?ie=UTF8&node=537504"
|
||||
}],
|
||||
"__next": "https://api.datamarket.azure.com/Data.ashx/Bing/Search/Web?Query='xbox'&$skip=2"
|
||||
}
|
||||
};
|
||||
|
||||
var should = require('should'),
|
||||
|
@ -32,62 +32,69 @@ var should = require('should'),
|
|||
bing = require('../lib/bing');
|
||||
|
||||
describe('Bing', function () {
|
||||
var server;
|
||||
var app;
|
||||
var port = 4321;
|
||||
var server;
|
||||
var app;
|
||||
var port = 4321;
|
||||
|
||||
before(function (done) {
|
||||
app = express();
|
||||
server = http.createServer(app);
|
||||
server.listen.apply(server, [port,
|
||||
function (err, result) {
|
||||
if (err) {
|
||||
done(err);
|
||||
} else {
|
||||
done();
|
||||
}
|
||||
}
|
||||
]);
|
||||
before(function (done) {
|
||||
app = express();
|
||||
server = http.createServer(app);
|
||||
server.listen.apply(server, [port,
|
||||
function (err, result) {
|
||||
if (err) {
|
||||
done(err);
|
||||
} else {
|
||||
done();
|
||||
}
|
||||
}
|
||||
]);
|
||||
});
|
||||
|
||||
after(function (done) {
|
||||
server.close();
|
||||
app = null;
|
||||
server = null;
|
||||
done();
|
||||
});
|
||||
|
||||
it('should cope with valid responses', function (done) {
|
||||
|
||||
app.get('/hello/Web', function (req, res) {
|
||||
res.status(200).send(JSON.stringify(validWebResponse));
|
||||
});
|
||||
|
||||
after(function (done) {
|
||||
server.close();
|
||||
app = null;
|
||||
server = null;
|
||||
done();
|
||||
var bingClient = bing({
|
||||
rootUri: 'http://localhost:' + port + '/hello/',
|
||||
accKey: '123'
|
||||
});
|
||||
|
||||
it('should cope with valid responses', function (done) {
|
||||
app.get('/hello/Web', function (req, res) {
|
||||
res.status(200).send(JSON.stringify(validWebResponse));
|
||||
});
|
||||
var bingClient = bing({
|
||||
rootUri: 'http://localhost:' + port + '/hello/',
|
||||
accKey: '123'
|
||||
});
|
||||
bingClient.search('xbox', function (error, response, body) {
|
||||
response.statusCode.should.eql(200);
|
||||
body.should.eql(validWebResponse);
|
||||
done();
|
||||
});
|
||||
bingClient.search('xbox', function (error, response, body) {
|
||||
response.statusCode.should.eql(200);
|
||||
body.should.eql(validWebResponse);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('should cope with errors', function (done) {
|
||||
|
||||
// No actual data on what the failure looks like.
|
||||
var failure = {
|
||||
message: 'Failed request'
|
||||
};
|
||||
|
||||
app.get('/hello/Image', function (req, res) {
|
||||
res.status(500).send(failure);
|
||||
});
|
||||
|
||||
var bingClient = bing({
|
||||
rootUri: 'http://localhost:' + port + '/hello/',
|
||||
accKey: '123'
|
||||
});
|
||||
|
||||
it('should cope with errors', function (done) {
|
||||
// No actual data on what the failure looks like.
|
||||
var failure = {
|
||||
message: 'Failed request'
|
||||
};
|
||||
app.get('/hello/Image', function (req, res) {
|
||||
res.status(500).send(failure);
|
||||
});
|
||||
var bingClient = bing({
|
||||
rootUri: 'http://localhost:' + port + '/hello/',
|
||||
accKey: '123'
|
||||
});
|
||||
bingClient.images('xbox', function (error, response, body) {
|
||||
response.statusCode.should.eql(500);
|
||||
body.should.eql(JSON.stringify(failure));
|
||||
done();
|
||||
});
|
||||
bingClient.images('xbox', function (error, response, body) {
|
||||
response.statusCode.should.eql(500);
|
||||
body.should.eql(JSON.stringify(failure));
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
|
@ -3,131 +3,145 @@
|
|||
// should still run the basic tests; thus throwing an exception must
|
||||
// be avoided.
|
||||
try {
|
||||
var accKey = require('./secrets').accKey;
|
||||
var accKey = require('./secrets').accKey;
|
||||
} catch (e) {
|
||||
console.log(e);
|
||||
console.log(e);
|
||||
}
|
||||
|
||||
if (!accKey) {
|
||||
return console.error("Need to include an access key in your secrets.js");
|
||||
return console.error("Need to include an access key in your secrets.js");
|
||||
}
|
||||
|
||||
|
||||
var Bing = require('../')({
|
||||
accKey: accKey
|
||||
});
|
||||
var should = require('should');
|
||||
var Bing = require('../')({ accKey: accKey })
|
||||
, should = require('should')
|
||||
|
||||
|
||||
describe("Bing Search", function () {
|
||||
|
||||
this.timeout(1000 * 10);
|
||||
this.timeout(1000 * 10);
|
||||
|
||||
it('works without options', function (done) {
|
||||
it('works without options', function (done) {
|
||||
|
||||
Bing.search('monkey vs frog', function (err, res, body) {
|
||||
Bing.search('monkey vs frog', function (err, res, body) {
|
||||
|
||||
should.not.exist(err);
|
||||
should.exist(res);
|
||||
should.exist(body);
|
||||
should.not.exist(err);
|
||||
should.exist(res);
|
||||
should.exist(body);
|
||||
|
||||
body.d.results.should.have.length(50);
|
||||
body.d.results.should.have.length(50);
|
||||
|
||||
//TODO check it contains the right fields
|
||||
done();
|
||||
});
|
||||
//TODO check it contains the right fields
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('finds only 5 results', function (done) {
|
||||
Bing.search('monkey vs frog', {
|
||||
top: 5,
|
||||
market: 'en-US',
|
||||
adult: 'Strict'
|
||||
}, function (err, res, body) {
|
||||
should.not.exist(err);
|
||||
should.exist(res);
|
||||
should.exist(body);
|
||||
it('finds only 5 results', function (done) {
|
||||
Bing.search('monkey vs frog',
|
||||
{
|
||||
top: 5,
|
||||
market: 'en-US',
|
||||
adult: 'Strict'
|
||||
},
|
||||
function (err, res, body) {
|
||||
|
||||
body.d.results.should.have.length(5);
|
||||
should.not.exist(err);
|
||||
should.exist(res);
|
||||
should.exist(body);
|
||||
|
||||
done();
|
||||
});
|
||||
body.d.results.should.have.length(5);
|
||||
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
|
||||
describe("Bing Images", function () {
|
||||
|
||||
this.timeout(1000 * 10);
|
||||
this.timeout(1000 * 10);
|
||||
|
||||
it('finds images with specific options', function (done) {
|
||||
Bing.images('pizza', {
|
||||
top: 3,
|
||||
adult: 'Off',
|
||||
imagefilters: {
|
||||
size: 'small',
|
||||
color: 'monochrome'
|
||||
}
|
||||
}, function (err, res, body) {
|
||||
should.not.exist(err);
|
||||
should.exist(res);
|
||||
should.exist(body);
|
||||
it('finds images with specific options', function (done) {
|
||||
Bing.images('pizza',
|
||||
{
|
||||
top: 3,
|
||||
adult: 'Off',
|
||||
imagefilters: {
|
||||
size: 'small',
|
||||
color: 'monochrome'
|
||||
}
|
||||
},
|
||||
function (err, res, body) {
|
||||
|
||||
body.d.results.should.have.length(3);
|
||||
should.not.exist(err);
|
||||
should.exist(res);
|
||||
should.exist(body);
|
||||
|
||||
done();
|
||||
});
|
||||
body.d.results.should.have.length(3);
|
||||
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
|
||||
describe("Bing News", function () {
|
||||
|
||||
this.timeout(1000 * 10);
|
||||
this.timeout(1000 * 10);
|
||||
|
||||
it('finds news with specific options', function (done) {
|
||||
it('finds news with specific options', function (done) {
|
||||
|
||||
Bing.news('ps4', {
|
||||
top: 10,
|
||||
skip: 1,
|
||||
newsortby: 'Date'
|
||||
}, function (err, res, body) {
|
||||
//TODO try unaccepted options like imagefilters
|
||||
Bing.news('ps4',
|
||||
{
|
||||
top: 10,
|
||||
skip: 1,
|
||||
newsortby: 'Date'
|
||||
},
|
||||
function (err, res, body) {
|
||||
|
||||
should.not.exist(err);
|
||||
should.exist(res);
|
||||
should.exist(body);
|
||||
//TODO try unaccepted options like imagefilters
|
||||
|
||||
body.d.results.should.have.length(10);
|
||||
should.not.exist(err);
|
||||
should.exist(res);
|
||||
should.exist(body);
|
||||
|
||||
done();
|
||||
});
|
||||
body.d.results.should.have.length(10);
|
||||
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
|
||||
describe("Bing Video", function () {
|
||||
|
||||
this.timeout(1000 * 10);
|
||||
this.timeout(1000 * 10);
|
||||
|
||||
it('finds videos with specific options', function (done) {
|
||||
it('finds videos with specific options', function (done) {
|
||||
|
||||
Bing.video('monkey vs frog', {
|
||||
top: 10
|
||||
}, function (err, res, body) {
|
||||
should.not.exist(err);
|
||||
should.exist(res);
|
||||
should.exist(body);
|
||||
Bing.video('monkey vs frog',
|
||||
{
|
||||
top: 10,
|
||||
videofilters: {
|
||||
duration: 'short',
|
||||
resolution: 'high'
|
||||
}
|
||||
},
|
||||
function (err, res, body) {
|
||||
|
||||
body.d.results.should.have.length(10);
|
||||
should.not.exist(err);
|
||||
should.exist(res);
|
||||
should.exist(body);
|
||||
|
||||
//TODO try here unaccepted options like imagefilters
|
||||
body.d.results.should.have.length(10);
|
||||
|
||||
done();
|
||||
});
|
||||
//TODO try here unaccepted options like imagefilters
|
||||
|
||||
done();
|
||||
});
|
||||
|
||||
});
|
||||
});
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue