Skip to content
Permalink
Browse files
fix(local-server): trying to access data of a server that's not runni…
…ng throws a meaningful error
  • Loading branch information
jan-molak committed Jan 19, 2020
1 parent e5cf6e8 commit ff6d012fd6e7c0b069a0afd3cc76250d8b828dfe
@@ -2,7 +2,7 @@ import 'mocha';

import { certificates, expect, stage } from '@integration/testing-tools';
import { Ensure, equals, startsWith } from '@serenity-js/assertions';
import { Actor, DressingRoom } from '@serenity-js/core';
import { Actor, DressingRoom, LogicError } from '@serenity-js/core';
import { CallAnApi, GetRequest, LastResponse, Send } from '@serenity-js/rest';
import axios from 'axios';
import * as https from 'https';
@@ -45,6 +45,26 @@ describe('ManageALocalServer', () => {
)).to.be.fulfilled; // tslint:disable-line:no-unused-expression
});

given(servers).
it('complains when the actor tries to access a URL of a server that is not running', function ({ handler, node }) {
if (! satisfies(process.versions.node, node)) {
return this.skip();
}

class Actors implements DressingRoom {
prepare(actor: Actor): Actor {
return actor.whoCan(
ManageALocalServer.runningAHttpListener(handler()),
);
}
}

Nadia = stage(new Actors()).theActorCalled('Nadia');

return expect(Nadia.attemptsTo(
Ensure.that(LocalServer.url(), startsWith('http://127.0.0.1')),
)).to.be.rejectedWith(LogicError, 'The server has not been started yet');
});
});

// ---
@@ -28,6 +28,10 @@ export class StopLocalServer extends Interaction {
*/
performAs(actor: UsesAbilities & CollectsArtifacts & AnswersQuestions): Promise<void> {
return ManageALocalServer.as(actor).mapInstance(server => new Promise((resolve, reject) => {
if (! server.address()) {
return resolve();
}

server.shutdown((error: Error) => {
if (!! error) {
return reject(error);
@@ -13,6 +13,10 @@ export class LocalServer {
return ManageALocalServer.as(actor).mapInstance((server, protocol) => {
const info = server.address();

if (! info) {
throw new LogicError(`The server has not been started yet`);
}

if (! isAddressInfo(info)) {
throw new LogicError(`A pipe or UNIX domain socket server does not have a URL`);
}

0 comments on commit ff6d012

Please sign in to comment.