abstract class sap.ui.core.util.MockServer

Visiblity: public
UX Guidelines:
Implements:
Available since: N/A
Module: sap/ui/core/util/MockServer
Application Component: CA-WDE-MCK

Class to mock http requests made to a remote server supporting the OData V2 REST protocol.


Constructor

Creates a mocked server. This helps to mock all or some back-end calls, e.g. for OData V2/JSON Models or simple XHR calls, without changing the application code. This class can also be used for qunit tests.

Accepts an object literal mSettings that defines initial property values, aggregated and associated objects as well as event handlers. See sap.ui.base.ManagedObject#constructor for a general description of the syntax of the settings object.

new sap.ui.core.util.MockServer(sId?, mSettings?, oScope?)
Param Type Default Value Description
sId? string

id for the new server object; generated automatically if no non-empty id is given Note: this can be omitted, no matter whether mSettings will be given or not!

mSettings? object

optional map/JSON-object with initial property values, aggregated objects etc. for the new object

oScope? object

scope object for resolving string based type and formatter references in bindings


Properties

Name Type Default Value Description
recordRequests boolean true

Whether or not the requests performed should be recorded (stored). This could be memory intense if each request is recorded. For unit testing purposes it should be set to true to compare requests performed otherwise this flag should be set to false e.g. for demonstration/app purposes.

Visibility: public
requests object[]

Getter for property requests.

Default value is []

Visibility: public
rootUri string

Getter for property rootUri. Has to be relative and requires a trailing '/'. It also needs to match the URI set in OData/JSON models or simple XHR calls in order for the mock server to intercept them.

Default value is empty/undefined. Must end with a a trailing slash ("/").

Visibility: public

Methods Overview

Method Description
attachAfter

Attaches an event handler to be called after the built-in request processing of the mock server

attachBefore

Attaches an event handler to be called before the built-in request processing of the mock server

sap.ui.core.util.MockServer.config

Global configuration of all mock servers.

destroy

Cleans up the resources associated with this object and all its aggregated children.

After an object has been destroyed, it can no longer be used!

Applications should call this method if they don't need the object any longer.

References:

  • sap.ui.base.ManagedObject#destroy

sap.ui.core.util.MockServer.destroyAll

Stops and calls destroy on all registered servers. Use this method for cleaning up.

detachAfter

Removes a previously attached event handler

detachBefore

Removes a previously attached event handler

sap.ui.core.util.MockServer.extend

Creates a new subclass of class sap.ui.core.util.MockServer with name sClassName and enriches it with the information contained in oClassInfo.

oClassInfo might contain the same kind of information as described in sap.ui.base.ManagedObject.extend.

getEntitySetData

Returns the data model of the given EntitySet name.

sap.ui.core.util.MockServer.getMetadata

Returns a metadata object for class sap.ui.core.util.MockServer.

getRecordRequests

Getter for property recordRequests. Returns whether or not the requests performed should be recorded (stored).

Default value is true

getRequests

Getter for property requests.

Default value is []

getRootUri

Getter for property rootUri. Has to be relative and requires a trailing '/'. It also needs to match the URI set in OData/JSON models or simple XHR calls in order for the mock server to intercept them.

Default value is empty/undefined. Must end with a a trailing slash ("/").

isStarted

Returns whether the server is started or not.

sap.ui.core.util.MockServer.respond

Respond to a request, when the servers are configured not to automatically respond.

setEntitySetData

Sets the data of the given EntitySet name with the given array.

setRecordRequests

Setter for property recordRequests. Defines whether or not the requests performed should be recorded (stored).

Default value is true

setRequests

Setter for property requests.

Default value is is []

Each array entry should consist of an object with the following properties / values:

  • method : "GET"|"POST"|"DELETE|"PUT"
    (any HTTP verb)
  • path : "/path/to/resource"
    The path is converted to a regular expression, so it can contain normal regular expression syntax. All regular expression groups are forwarded as arguments to the response function. In addition to this, parameters can be written in this notation: :param. These placeholder will be replaced by regular expression groups.
  • response : function(xhr, param1, param2, ...) { }
    The xhr object can be used to respond on the request. Supported methods are:
    xhr.respond(iStatusCode, mHeaders, sBody)
    xhr.respondJSON(iStatusCode, mHeaders, oJsonObjectOrString). By default a JSON header is set for response header
    xhr.respondXML(iStatusCode, mHeaders, sXmlString). By default an XML header is set for response header
    xhr.respondFile(iStatusCode, mHeaders, sFileUrl). By default the mime type of the file is set for response header

setRootUri

Setter for property rootUri. All request path URI are prefixed with this root URI if set.

Default value is empty/undefined

simulate

Simulates an existing OData service by sepcifying the metadata URL and the base URL for the mockdata. The server configures the request handlers depending on the service metadata. The mockdata needs to be stored individually for each entity type in a separate JSON file. The name of the JSON file needs to match the name of the entity type. If no base url for the mockdata is specified then the mockdata are generated from the metadata

start

Starts the server.

sap.ui.core.util.MockServer.startAll

Starts all registered servers.

stop

Stops the server.

sap.ui.core.util.MockServer.stopAll

Stops all registered servers.

attachAfter

Attaches an event handler to be called after the built-in request processing of the mock server

Param Type DefaultValue Description
event string

type according to HTTP Method

fnCallback function

the name of the function that will be called at this exit The callback function exposes an event with parameters, depending on the type of the request. oEvent.getParameters() lists the parameters as per the request. Examples are: oXhr : the request object; oFilteredData : the mock data entries that are about to be returned in the response; oEntry : the mock data entry that is about to be returned in the response;

sEntitySet string

(optional) the name of the entity set

attachBefore

Attaches an event handler to be called before the built-in request processing of the mock server

Param Type DefaultValue Description
event string

type according to HTTP Method

fnCallback function

the name of the function that will be called at this exit. The callback function exposes an event with parameters, depending on the type of the request. oEvent.getParameters() lists the parameters as per the request. Examples are: oXhr : the request object; sUrlParams : the URL parameters of the request; sKeys : key properties of the requested entry; sNavProp/sNavName : name of navigation

sEntitySet string

(optional) the name of the entity set

sap.ui.core.util.MockServer.config

Global configuration of all mock servers.

Param Type DefaultValue Description
mConfig object

the configuration object.

autoRespond boolean true

If set true, all mock servers will respond automatically. If set false you have to call sap.ui.core.util.MockServer.respond method for response.

autoRespondAfter int 0

the time in ms after all mock servers should send their response.

fakeHTTPMethods boolean false

If set to true, all mock server will find _method parameter in the POST body and use this to override the actual method.

destroy

Cleans up the resources associated with this object and all its aggregated children.

After an object has been destroyed, it can no longer be used!

Applications should call this method if they don't need the object any longer.

References:

Param Type DefaultValue Description
bSuppressInvalidate boolean

if true, this ManagedObject is not marked as changed

sap.ui.core.util.MockServer.destroyAll

Stops and calls destroy on all registered servers. Use this method for cleaning up.

detachAfter

Removes a previously attached event handler

Param Type DefaultValue Description
event string

type according to HTTP Method

fnCallback function

the name of the function that will be called at this exit

sEntitySet string

(optional) the name of the entity set

detachBefore

Removes a previously attached event handler

Param Type DefaultValue Description
event string

type according to HTTP Method

fnCallback function

the name of the function that will be called at this exit

sEntitySet string

(optional) the name of the entity set

sap.ui.core.util.MockServer.extend

Creates a new subclass of class sap.ui.core.util.MockServer with name sClassName and enriches it with the information contained in oClassInfo.

oClassInfo might contain the same kind of information as described in sap.ui.base.ManagedObject.extend.

Param Type DefaultValue Description
sClassName string

Name of the class being created

oClassInfo object

Object literal with information about the class

FNMetaImpl function

Constructor function for the metadata object; if not given, it defaults to the metadata implementation used by this class

getEntitySetData

Returns the data model of the given EntitySet name.

Param Type DefaultValue Description
sEntitySetName string

EntitySet name

sap.ui.core.util.MockServer.getMetadata

Returns a metadata object for class sap.ui.core.util.MockServer.

getRecordRequests

Getter for property recordRequests. Returns whether or not the requests performed should be recorded (stored).

Default value is true

getRequests

Getter for property requests.

Default value is []

getRootUri

Getter for property rootUri. Has to be relative and requires a trailing '/'. It also needs to match the URI set in OData/JSON models or simple XHR calls in order for the mock server to intercept them.

Default value is empty/undefined. Must end with a a trailing slash ("/").

isStarted

Returns whether the server is started or not.

sap.ui.core.util.MockServer.respond

Respond to a request, when the servers are configured not to automatically respond.

setEntitySetData

Sets the data of the given EntitySet name with the given array.

Param Type DefaultValue Description
sEntitySetName string

EntitySet name

aData array

setRecordRequests

Setter for property recordRequests. Defines whether or not the requests performed should be recorded (stored).

Default value is true

Param Type DefaultValue Description
recordRequests boolean

new value for property recordRequests

setRequests

Setter for property requests.

Default value is is []

Each array entry should consist of an object with the following properties / values:

Param Type DefaultValue Description
requests object[]

new value for property requests

setRootUri

Setter for property rootUri. All request path URI are prefixed with this root URI if set.

Default value is empty/undefined

Param Type DefaultValue Description
rootUri string

new value for property rootUri

simulate

Simulates an existing OData service by sepcifying the metadata URL and the base URL for the mockdata. The server configures the request handlers depending on the service metadata. The mockdata needs to be stored individually for each entity type in a separate JSON file. The name of the JSON file needs to match the name of the entity type. If no base url for the mockdata is specified then the mockdata are generated from the metadata

Param Type DefaultValue Description
sMetadataString string

Either the URL to the service metadata document or the metadata document as xml string itself (starting with "

vMockdataSettings string object

(optional) base url which contains the path to the mockdata, or an object which contains the following properties: sMockdataBaseUrl, bGenerateMissingMockData, aEntitySetsNames. See below for descriptions of these parameters. Ommit this parameter to produce random mock data based on the service metadata.

sMockdataBaseUrl string

base url which contains the mockdata as single .json files or the .json file containing the complete mock data

bGenerateMissingMockData boolean false

if the MockServer should generate mock data for missing .json files that are not found in sMockdataBaseUrl

aEntitySetsNames array []

list of entity set names to fetch. If the list is empty or undefined, all entity sets will be retrieved. This parameter should be used to improve performance in case there are a lot of entity sets but only a few are needed to be fetched.

start

Starts the server.

sap.ui.core.util.MockServer.startAll

Starts all registered servers.

stop

Stops the server.

sap.ui.core.util.MockServer.stopAll

Stops all registered servers.