import {ManageALocalServer} from '@serenity-js/local-server/lib/screenplay/abilities'
public class | source

ManageALocalServer

An Ability that enables the Actor to manage a local Node.js server.

Implements:

Examples:

Using a raw Node.js server
import { Actor } from '@serenity-js/core';
import { CallAnApi, GetRequest, Send } from '@serenity-js/rest';
import { ManageALocalServer, LocalTestServer, StartLocalTestServer, StopLocalTestServer } from '@serenity-js/local-server'
import { Ensure, equals } from '@serenity-js/assertions';

import axios from 'axios';
import * as http from 'http';

const server = http.createServer(function (request, response) {
    response.setHeader('Connection', 'close');
    response.end('Hello!');
})

const actor = Actor.named('Apisit').whoCan(
    ManageALocalServer.using(server),
    CallAnApi.using(axios.create()),
);

actor.attemptsTo(
    StartLocalTestServer.onRandomPort(),
    Send.a(GetRequest.to(LocalServer.url())),
    Ensure.that(LastResponse.status(), equals(200)),
    Ensure.that(LastResponse.body<string>(), equals('Hello!')),
    StopLocalTestServer.ifRunning(),
);

Static Method Summary

Static Public Methods
public static

Used to access the Actor's Ability to ManageALocalServer from within the Interaction classes, such as StartLocalServer.

public static

Ability to manage a Node.js HTTP server using the provided requestListener.

public static

runningAHttpsListener(listener: Server, options: ServerOptions): ManageALocalServer

Ability to manage a Node.js HTTPS server using the provided server requestListener.

Constructor Summary

Public Constructor
public

constructor(protocol: string, server: Server)

Method Summary

Public Methods
public

listen(preferredPort: number, highestPort: number): Promise<void>

Starts the server on the first free port between preferredPort and highestPort, inclusive.

public

mapInstance(fn: function(server: ServerWithShutdown, protocol?: SupportedProtocols): T): T

Provides access to the server requestListener

Static Public Methods

public static as(actor: UsesAbilities): ManageALocalServer source

Used to access the Actor's Ability to ManageALocalServer from within the Interaction classes, such as StartLocalServer.

Params:

NameTypeAttributeDescription
actor UsesAbilities

public static runningAHttpListener(listener: Server): ManageALocalServer source

Ability to manage a Node.js HTTP server using the provided requestListener.

Params:

NameTypeAttributeDescription
listener Server

public static runningAHttpsListener(listener: Server, options: ServerOptions): ManageALocalServer source

Ability to manage a Node.js HTTPS server using the provided server requestListener.

Params:

NameTypeAttributeDescription
listener Server
options ServerOptions

Accepts options from tls.createServer(), tls.createSecureContext() and http.createServer().

Public Constructors

public constructor(protocol: string, server: Server) source

Params:

NameTypeAttributeDescription
protocol string

Protocol to be used when communicating with the running server; http or https

server Server

A Node.js server requestListener, with support for server shutdown.

Public Methods

public listen(preferredPort: number, highestPort: number): Promise<void> source

Starts the server on the first free port between preferredPort and highestPort, inclusive.

Params:

NameTypeAttributeDescription
preferredPort number
  • optional
  • default: 8000

Lower bound of the preferred port range

highestPort number
  • optional
  • default: 65535

highestPort Upper bound of the preferred port range

Returns:

Promise<void>

public mapInstance(fn: function(server: ServerWithShutdown, protocol?: SupportedProtocols): T): T source

Provides access to the server requestListener

Params:

NameTypeAttributeDescription
fn function(server: ServerWithShutdown, protocol?: SupportedProtocols): T

Returns:

T