Class to mock http requests made to a remote server supporting the OData V2 REST protocol.
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? | 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 |
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 |
requests | object[] | Getter for property Default value is |
|
rootUri | string | Getter for property Default value is empty/ |
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.
|
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
|
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 Default value is |
getRequests |
Getter for property Default value is |
getRootUri |
Getter for property Default value is empty/ |
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 Default value is |
setRequests |
Setter for property Default value is is Each array entry should consist of an object with the following properties / values:
|
setRootUri |
Setter for property Default value is empty/ |
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. |
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 |
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 |
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 |
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 |
Stops and calls destroy on all registered servers. Use this method for cleaning up.
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 |
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 |
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 |
Returns the data model of the given EntitySet name.
Param | Type | DefaultValue | Description |
---|---|---|---|
sEntitySetName | string |
EntitySet name |
Returns a metadata object for class sap.ui.core.util.MockServer.
Getter for property recordRequests
. Returns whether or not the requests performed should be recorded (stored).
Default value is true
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 ("/").
Respond to a request, when the servers are configured not to automatically respond.
Sets the data of the given EntitySet name with the given array.
Param | Type | DefaultValue | Description |
---|---|---|---|
sEntitySetName | string |
EntitySet name |
|
aData | array |
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 |
Setter for property requests
.
Default value is is []
Each array entry should consist of an object with the following properties / values:
response
function. In addition to this, parameters can be written in this notation: :param
. These placeholder will be replaced by regular expression groups. 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 Param | Type | DefaultValue | Description |
---|---|---|---|
requests | object[] |
new value for property |
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 |
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 |