<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>localService</title>
<script
id="sap-ui-bootstrap"
src="/resources/sap-ui-core.js"
data-sap-ui-async="true"
data-sap-ui-resourceroots='{
"localService": "./localService"
}'
data-sap-ui-oninit="module:localService/mockserver"
></script>
</head>
<body>
<div>localService</div>
</body>
</html>
sap.ui.define([
'sap/ui/core/util/MockServer'
], (MockServer) => {
'use strict';
let oMockServer = new MockServer({
rootUri: window.__ROOT_URI // will be injected by puppeteer
});
let sMetadataString = sap.ui.require.toUrl("localService/metadata.xml");
var sMockdataBaseUrl = sap.ui.require.toUrl("localService/mockdata");
oMockServer.simulate(sMetadataString, {
sMockdataBaseUrl: sMockdataBaseUrl
});
oMockServer.start();
});
{
"name": "ui5-mockserver-puppeteer",
"version": "0.1.0",
"main": "index.js",
"dependencies": {
"@openui5/sap.ui.core": "^1.62.1",
"@ui5/project": "^1.0.0",
"@ui5/server": "^1.0.0",
"body-parser": "^1.18.3",
"express": "^4.16.4",
"morgan": "^1.9.1",
"puppeteer": "^1.12.2"
},
"config": {
"port": 3000
},
"scripts": {
"start" : "node index.js"
}
}
'use strict'
const normalizer = require('@ui5/project').normalizer;
const server = require('@ui5/server').server;
module.exports = async options => {
let tree = await normalizer.generateProjectTree({
cwd: options.cwd
});
return server.serve(tree, {
port: options.port,
changePortIfInUse: true,
acceptRemoteConnections: false
});
};
'use strict';
// load modules
const puppeteer = require('puppeteer');
// load lib
const ui5Serve = require('./ui5-serve');
module.exports = class UI5Page {
constructor(options) {
this._cwd = options.cwd;
this._port = options.port;
this._rootUri = options.rootUri;
this._page = null;
}
async _loadPage() {
// start server & launch browser
let [server, browser] = await Promise.all([
ui5Serve({
cwd: this._cwd,
port: this._port
}),
puppeteer.launch()
]);
// open new page
let page = await browser.newPage();
// inject root uri
page.evaluateOnNewDocument(rootUri => {
Object.assign(window, {
__ROOT_URI: rootUri
});
}, this._rootUri);
// go to page
await page.goto(`http://localhost:${server.port}/index.html`, {
waitUntil: 'networkidle0' // ensure page has been fully loaded
});
return page;
}
async _evaluate() {
// lazily load page
if (!this._page) {
this._page = await this._loadPage();
}
return this._page.evaluate.apply(this._page, [].slice.call(arguments));
}
ajax(reqMethod, reqUrl, reqBody) {
return this._evaluate((method, url, body) => {
// will be evaluated in the page's context
return new Promise((resolve, reject) => {
// trigger ajax request which will be intercepted by mockserver
$.ajax({
method: method,
url: url,
data: body,
dataType: 'text'
})
.done((data, textStatus, jqXHR) => resolve({
data: data,
contentType: jqXHR.getResponseHeader('Content-Type')
}))
.fail((jqXHR, textStatus, errorThrown) => reject(new Error(errorThrown)));
});
}, reqMethod, reqUrl, reqBody);
}
}
'use strict';
// load modules
const express = require('express');
const bodyParser = require('body-parser')
const puppeteer = require('puppeteer');
// load lib
const UI5Page = require('./UI5Page');
module.exports = options => {
// create new page (incl. mock server)
let ui5Page = new UI5Page(options);
// a router object is an isolated instance of middleware and routes
let router = express.Router();
// parse any request body as text for forwarding
router.use(bodyParser.text({
type: '*/*'
}));
// handle all incoming requests
router.all('/*', async (req, res, next) => {
try {
// forward request to page
let result = await ui5Page.ajax(req.method, req.originalUrl, req.body);
// send response with result returned by page
res.set('Content-Type', result.contentType)
res.end(result.data);
} catch (error) {
next(error);
}
});
return router;
};
'use strict';
// load modules
const express = require('express');
const morgan = require('morgan');
// load lib
const ui5Router = require('./lib/ui5-router');
// config
const PORT = parseInt(process.env.npm_package_config_port);
const ROOT_URI = "/sap/opu/odata/sap/MEETUP_SRV/";
// create new app
let app = express();
// log incoming requests
app.use(morgan('dev'));
// forward requests to mockserver
app.use(ROOT_URI, ui5Router({
cwd: __dirname,
port: PORT + 1, // port for ui5 server (only used internally)
rootUri: ROOT_URI
}));
// start server
let server = app.listen(PORT, () => console.log(`app is listening at http://localhost:${PORT}`));
server.on('error', error => console.error(`${error.message}`));
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<edmx:Edmx Version="1.0"
xmlns:edmx="http://schemas.microsoft.com/ado/2007/06/edmx">
<edmx:DataServices
xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" m:DataServiceVersion="1.0">
<Schema Namespace="NerdMeetup.Models"
xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices"
xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata"
xmlns="http://schemas.microsoft.com/ado/2006/04/edm">
<EntityType Name="Meetup">
<Key>
<PropertyRef Name="MeetupID" />
</Key>
<Property Name="MeetupID" Type="Edm.Int32" Nullable="false" />
<Property Name="Title" Type="Edm.String" Nullable="true" />
<Property Name="EventDate" Type="Edm.DateTime" Nullable="false" />
<Property Name="Description" Type="Edm.String" Nullable="true" />
<Property Name="HostedBy" Type="Edm.String" Nullable="true" />
<Property Name="ContactPhone" Type="Edm.String" Nullable="true" />
<Property Name="Address" Type="Edm.String" Nullable="true" />
<Property Name="Country" Type="Edm.String" Nullable="true" />
<Property Name="Latitude" Type="Edm.Double" Nullable="false" />
<Property Name="Longitude" Type="Edm.Double" Nullable="false" />
<Property Name="HostedById" Type="Edm.String" Nullable="true" />
<Property Name="Location" Type="NerdMeetup.Models.LocationDetail" Nullable="false" />
</EntityType>
<ComplexType Name="LocationDetail" />
<EntityContainer Name="NerdMeetups" m:IsDefaultEntityContainer="true">
<EntitySet Name="Meetups" EntityType="NerdMeetup.Models.Meetup" />
<FunctionImport Name="FindUpcomingMeetups" EntitySet="Meetups" ReturnType="Collection(NerdMeetup.Models.Meetup)" m:HttpMethod="GET" />
</EntityContainer>
</Schema>
</edmx:DataServices>
</edmx:Edmx>
{"d":{"results":[{"MeetupID":1,"Title":"Toronto Tech Meet-Up","EventDate":"/Date(1593810000000)/","Description":"The best way to expand your knowledge and network of the Toronto technology community","__metadata":{"id":"/sap/opu/odata/sap/MEETUP_SRV/Meetups(1)","type":"NerdMeetup.Models.Meetup","uri":"/sap/opu/odata/sap/MEETUP_SRV/Meetups(1)"}}]}}
? Happy Coding ?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
38 | |
19 | |
13 | |
13 | |
10 | |
10 | |
10 | |
8 | |
8 | |
8 |