UNPKG

3.24 kBJavaScriptView Raw
1'use strict';
2
3var octo = require('./index');
4var fs = require('fs');
5var expect = require('chai').expect;
6var sinon = require('sinon');
7var request = require('request');
8
9describe('push', function() {
10 var postStub;
11
12 beforeEach(function(){
13 postStub = sinon.stub(request, 'post');
14 });
15
16 afterEach(function(){
17 postStub.restore();
18 });
19
20 it('should pass pkg stream', function() {
21 octo.push(new Buffer('hello world'), {
22 apikey: 'KEY',
23 host: 'http://localhost',
24 name: 'package.tar'
25 });
26
27 var req = postStub.firstCall.args[0];
28 expect(req.headers['X-Octopus-ApiKey']).to.equal('KEY');
29 expect(req.formData.file.value.toString()).to.equal(new Buffer('hello world').toString());
30 expect(req.formData.file.options.filename).to.equal('package.tar');
31 });
32
33 describe('build url', function () {
34 it('should include `replace` parameter if it is provided', function () {
35 octo.push(new Buffer('hello world'), { replace: true, host: 'http://myweb/' });
36 var req = postStub.lastCall.args[0];
37 expect(req.url).to.equal('http://myweb/api/packages/raw?replace=true');
38 });
39
40 it('should build correct url regardless of trailing slash', function () {
41 testUrl('http://myweb', 'http://myweb/api/packages/raw');
42 testUrl('http://myweb/', 'http://myweb/api/packages/raw');
43 });
44
45 it('should build correct url with port', function () {
46 testUrl('http://myweb:3000/', 'http://myweb:3000/api/packages/raw')
47 });
48
49 it('should build correct url with relative path', function () {
50 testUrl('http://myweb/path/to/octopus', 'http://myweb/path/to/octopus/api/packages/raw');
51 });
52
53 function testUrl(host, expected) {
54 octo.push(new Buffer('hello world'), { host: host });
55 var req = postStub.lastCall.args[0];
56 expect(req.url).to.equal(expected);
57 }
58 });
59
60 it('should return response body if request successful', function(done) {
61 var body = { prop: 12 };
62
63 octo.push(new Buffer('hello world'), {
64 apikey: 'KEY',
65 replace: true,
66 host: 'http://localhost'
67 }, function(err, result) {
68 expect(err).to.be.null;
69 expect(result).to.eql(body);
70 done();
71 });
72
73 var callback = postStub.firstCall.args[1];
74 callback(null, {statusCode: 200}, body);
75 });
76});
77
78describe('pack', function() {
79
80 it('can create zip', function (done) {
81 octo.pack()
82 .append('buffer files/hello.txt', new Buffer('hello world'), {date: new Date(2011, 11, 11)})
83 .append('stream.txt', fs.createReadStream('./package.json'))
84 .append('lib/pack.js')
85 .toStream(function (err, data) {
86 expect(err).to.be.null;
87 expect(data.stream.readable).to.be.true;
88 expect(data.name).not.to.be.null;
89 done();
90 });
91 });
92
93 it('can create nupkg', function (done) {
94 octo.pack('nupkg')
95 .append('buffer files/hello.txt', new Buffer('hello world'), {date: new Date(2011, 11, 11)})
96 .append('stream.txt', fs.createReadStream('./package.json'))
97 .append('lib/pack.js')
98 .toFile('./bin', function (err, data) {
99 expect(err).to.be.null;
100 expect(data.path).not.to.be.null;
101 expect(data.name).not.to.be.null;
102 done();
103 });
104 });
105});