Skip to content

Commit

Permalink
add configurable endpoint and port.
Browse files Browse the repository at this point in the history
the bucket was removed from the domain so that alternate hosts with s3 compatible APIs could be used.

also, the putFile test adds /test/user2.json so that the deleteFile test has a valid file to delete.  Tests were blowing up.
  • Loading branch information
technoweenie committed Jul 8, 2011
1 parent 5c5a9f8 commit ca5868d
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 11 deletions.
10 changes: 5 additions & 5 deletions lib/knox/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ var utils = require('./utils')
*/

var Client = module.exports = exports = function Client(options) {
this.endpoint = 's3.amazonaws.com';
this.port = 80;
if (!options.key) throw new Error('aws "key" required');
if (!options.secret) throw new Error('aws "secret" required');
if (!options.bucket) throw new Error('aws "bucket" required');

this.endpoint = options.bucket + '.s3.amazonaws.com';
utils.merge(this, options);
};

Expand All @@ -50,7 +50,7 @@ var Client = module.exports = exports = function Client(options) {
*/

Client.prototype.request = function(method, filename, headers){
var options = { host: this.endpoint, port: 80 }
var options = { host: this.endpoint, port: this.port }
, date = new Date
, headers = headers || {};

Expand All @@ -73,7 +73,7 @@ Client.prototype.request = function(method, filename, headers){

// Issue request
options.method = method;
options.path = join('/', filename);
options.path = join('/', this.bucket, filename);
options.headers = headers;
var req = http.request(options);
req.url = this.url(filename);
Expand Down Expand Up @@ -304,7 +304,7 @@ Client.prototype.deleteFile = function(filename, headers, fn){

Client.prototype.url =
Client.prototype.http = function(filename){
return 'http://' + join(this.endpoint, filename);
return 'http://' + join(this.endpoint, this.bucket, filename);
};

/**
Expand Down
12 changes: 6 additions & 6 deletions test/knox.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ module.exports = {
assert.equal('foobar', client.key);
assert.equal('baz', client.secret);
assert.equal('misc', client.bucket);
assert.equal('misc.s3.amazonaws.com', client.endpoint);
assert.equal('s3.amazonaws.com', client.endpoint);
},

'test .createClient() custom endpoint': function(assert){
Expand All @@ -74,10 +74,10 @@ module.exports = {

'test .putFile()': function(assert, done){
var n = 0;
client.putFile(jsonFixture, '/test/user.json', function(err, res){
client.putFile(jsonFixture, '/test/user2.json', function(err, res){
assert.ok(!err, 'putFile() got an error!');
assert.equal(200, res.statusCode);
client.get('/test/user.json').on('response', function(res){
client.get('/test/user2.json').on('response', function(res){
assert.equal('application/json', res.headers['content-type']);
done();
}).end();
Expand All @@ -98,10 +98,10 @@ module.exports = {
req.on('response', function(res){
assert.equal(200, res.statusCode);
assert.equal(
'http://'+client.bucket+'.s3.amazonaws.com/test/user.json'
'http://'+client.endpoint+'/'+client.bucket+'/test/user.json'
, client.url('/test/user.json'));
assert.equal(
'http://'+client.bucket+'.s3.amazonaws.com/test/user.json'
'http://'+client.endpoint+'/'+client.bucket+'/test/user.json'
, req.url);
done();
});
Expand Down Expand Up @@ -165,7 +165,7 @@ module.exports = {
},

'test .deleteFile()': function(assert, done){
client.deleteFile('/test/user.json', function(err, res){
client.deleteFile('/test/user2.json', function(err, res){
assert.ok(!err);
assert.equal(204, res.statusCode);
done();
Expand Down

0 comments on commit ca5868d

Please sign in to comment.