Skip to content

Commit

Permalink
Merge pull request slackapi#28 from aoberoi/add-adapter-stop
Browse files Browse the repository at this point in the history
adds SlackEventAdapter.stop() method
  • Loading branch information
aoberoi committed Mar 20, 2019
1 parent ff6a529 commit 9bfe252
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 0 deletions.
18 changes: 18 additions & 0 deletions packages/node-slack-events-api/src/adapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,30 @@ export default class SlackEventAdapter extends EventEmitter {
start(port) {
return this.createServer()
.then(server => new Promise((resolve, reject) => {
this.server = server;
server.on('error', reject);
server.listen(port, () => resolve(server));
debug('server started - port: %s', port);
}));
}

stop() {
return new Promise((resolve, reject) => {
if (this.server) {
this.server.close((error) => {
delete this.server;
if (error) {
reject(error);
} else {
resolve();
}
});
} else {
reject(new Error('SlackEventAdapter cannot stop when it did not start a server'));
}
});
}

expressMiddleware(middlewareOptions = {}) {
return createExpressMiddleware(this, middlewareOptions);
}
Expand Down
45 changes: 45 additions & 0 deletions packages/node-slack-events-api/test/integration/test-basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,3 +130,48 @@ describe('when using middleware inside your own express application', function (
});
});
});

describe('when using the built-in HTTP server', function () {
beforeEach(function () {
this.port = process.env.PORT || '8080';
// This is the default path
this.path = '/slack/events';
this.verificationToken = 'VERIFICATION_TOKEN';
this.adapter = createSlackEventAdapter(this.verificationToken, {
waitForResponse: true
});
return this.adapter.start(this.port);
});

afterEach(function () {
return this.adapter.stop();
});

it('should be able to stop the built-in HTTP server', function () {
var self = this;
return self.adapter.stop()
.then(function () {
return self.adapter.start(self.port);
});
});

it('should not stop if there is no built-in HTTP server running', function (done) {
var self = this;
self.adapter.stop()
.then(function () {
self.adapter.stop()
.then(function () {
done(new Error('should not be able to stop an already stopped server'));
})
.catch(function (error) {
assert(error instanceof Error);
self.adapter.start(self.port)
.then(function () {
done();
})
.catch(done);
});
})
.catch(done);
});
});

0 comments on commit 9bfe252

Please sign in to comment.