Skip to content
This repository has been archived by the owner on Mar 8, 2020. It is now read-only.

Expose request to transaction processor functions (contributes to #3092) #3528

Merged
merged 2 commits into from
Mar 6, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion packages/composer-connector-web/karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,9 @@ module.exports = function(config) {
]
},
node: {
net: 'empty'
fs: 'empty',
net: 'empty',
tls: 'empty'
}
},

Expand Down
11 changes: 4 additions & 7 deletions packages/composer-playground/config/webpack.common.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,7 @@ module.exports = function(options) {
extensions: ['.ts', '.js', '.json'],

// An array of directory names to be resolved to the current directory
modules: [helpers.root('src'), 'node_modules'],

// Use our versions of Node modules.
alias: {
request$: require.resolve('xhr') // used for HTTP POST
}
modules: [helpers.root('src'), 'node_modules']

},

Expand Down Expand Up @@ -351,7 +346,9 @@ module.exports = function(options) {
],

node: {
fs: 'empty'
fs: 'empty',
net: 'empty',
tls: 'empty'
}
};
};
4 changes: 3 additions & 1 deletion packages/composer-playground/config/webpack.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,9 @@ module.exports = function (options) {
],

node: {
fs: 'empty'
fs: 'empty',
net: 'empty',
tls: 'empty'
}
};
};
4 changes: 3 additions & 1 deletion packages/composer-runtime-web/karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,9 @@ module.exports = function(config) {
]
},
node: {
net: 'empty'
fs: 'empty',
net: 'empty',
tls: 'empty'
}
},

Expand Down
1 change: 1 addition & 0 deletions packages/composer-runtime/lib/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,7 @@ class Api {
* @param {string} url The URL to post the data to
* @param {Typed} typed The typed instance to be posted. The instance will be serialized to JSON.
* @param {object} options The options that are passed to Serializer.toJSON
* @deprecated since v0.18.1, use the built-in request module instead
* @return {Promise} A promise. The promise is resolved with a HttpResponse
* that represents the result of the HTTP POST.
* @public
Expand Down
4 changes: 3 additions & 1 deletion packages/composer-runtime/lib/scriptcompiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const Api = require('./api');
const assert = require('assert');
const CompiledScriptBundle = require('./compiledscriptbundle');
const Logger = require('composer-common').Logger;
const request = require('request-promise-any');
const SourceMapConsumer = require('source-map').SourceMapConsumer;
const SourceMapGenerator = require('source-map').SourceMapGenerator;
const SourceNode = require('source-map').SourceNode;
Expand Down Expand Up @@ -51,7 +52,8 @@ class ScriptCompiler {

// Define the globals.
const globals = {
assert: assert
assert,
request
};

// Add the start section.
Expand Down
2 changes: 2 additions & 0 deletions packages/composer-runtime/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@
"debug": "2.6.2",
"fast-json-patch": "1.1.8",
"lru-cache": "4.0.2",
"request": "2.81.0",
"request-promise-any": "1.0.5",
"sha.js": "2.4.8",
"source-map": "0.5.6"
},
Expand Down
7 changes: 6 additions & 1 deletion packages/composer-runtime/test/scriptcompiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const Api = require('../lib/api');
const assert = require('assert');
const ModelManager = require('composer-common').ModelManager;
const path = require('path');
const request = require('request-promise-any');
const ScriptManager = require('composer-common').ScriptManager;
const ScriptCompiler = require('../lib/scriptcompiler');
const SourceMapConsumer = require('source-map').SourceMapConsumer;
Expand Down Expand Up @@ -55,6 +56,7 @@ describe('ScriptCompiler', () => {
*/
function doIt(transaction) {
assert.ok(true);
request.post('http://some.url');
getFactory();
emit();
}
Expand Down Expand Up @@ -97,6 +99,7 @@ describe('ScriptCompiler', () => {

it('should compile all of the scripts in the specified script manager into a bundle', () => {
sandbox.stub(assert, 'ok');
sandbox.stub(request, 'post');
const compiledScriptBundle = scriptCompiler.compile(scriptManager);
const functionDeclarations = compiledScriptBundle.functionDeclarations;
const generatorFunction = compiledScriptBundle.generatorFunction;
Expand All @@ -111,6 +114,8 @@ describe('ScriptCompiler', () => {
result.doIt();
sinon.assert.calledOnce(assert.ok);
sinon.assert.calledWith(assert.ok, true);
sinon.assert.calledOnce(request.post);
sinon.assert.calledWith(request.post, 'http://some.url');
sinon.assert.calledOnce(mockApi.getFactory);
sinon.assert.calledOnce(mockApi.emit);
});
Expand Down Expand Up @@ -168,7 +173,7 @@ describe('ScriptCompiler', () => {
sourceMapConsumer.eachMapping((mapping) => {
mappings.push(mapping);
});
mappings.should.have.lengthOf(37);
mappings.should.have.lengthOf(44);
sourceMapConsumer.sourceContentFor(path.resolve(process.cwd(), 'script1')).should.match(/function doIt3a\(transaction\) {/);
});

Expand Down
4 changes: 3 additions & 1 deletion packages/composer-tests-functional/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,6 @@ out
*.swp

# Build generated files should be ignored by git, but not by npm.
storage
storage
.pm2
http.port
4 changes: 3 additions & 1 deletion packages/composer-tests-functional/karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,9 @@ module.exports = function(config) {
]
},
node: {
net: 'empty'
fs: 'empty',
net: 'empty',
tls: 'empty'
}
},

Expand Down
11 changes: 8 additions & 3 deletions packages/composer-tests-functional/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
"postlicchk": "npm run lint",
"lint": "eslint .",
"test": "exit 0",
"start_verdaccio": "verdaccio -l 0.0.0.0:4873 -c verdaccio.cfg >/dev/null &",
"stop_verdaccio": "pkill verdaccio | true",
"start_verdaccio": "PM2_HOME=.pm2 pm2 start verdaccio -- -l 0.0.0.0:4873 -c verdaccio.cfg",
"stop_verdaccio": "PM2_HOME=.pm2 pm2 stop verdaccio | true",
"systest:embedded": "mocha -t 0 systest",
"systest:hlf": "mocha -t 0 systest",
"systest:hlfv1": "mocha -t 0 systest",
Expand All @@ -26,7 +26,9 @@
"systest:hlfv1-2_tls": "mocha -t 0 systest/[q-z]*.js systest/setup.js",
"systest:hlfv1_hsm": "mocha -t 0 systest/historian.js systest/accesscontrols.js systest/identities.js systest/setup.js",
"systest:proxy": "mocha -t 0 systest",
"systest:web": "karma start --single-run"
"systest:web": "karma start --single-run",
"start_http": "PM2_HOME=.pm2 pm2 start --wait-ready scripts/http.js",
"stop_http": "PM2_HOME=.pm2 pm2 stop scripts/http.js | true"
},
"repository": {
"type": "git",
Expand All @@ -44,6 +46,7 @@
"babel-loader": "6.2.10",
"babel-polyfill": "6.23.0",
"babel-preset-latest": "6.24.1",
"body-parser": "1.17.0",
"brfs": "1.4.3",
"browserfs": "1.1.0",
"chai": "3.5.0",
Expand All @@ -59,6 +62,7 @@
"composer-wallet-inmemory": "0.18.1",
"dockerode": "2.5.1",
"eslint": "3.17.1",
"express": "4.15.2",
"homedir": "0.6.0",
"karma": "1.3.0",
"karma-chai": "0.1.0",
Expand All @@ -69,6 +73,7 @@
"license-check": "1.1.5",
"mkdirp": "0.5.1",
"mocha": "3.4.2",
"pm2": "2.10.1",
"sleep-promise": "2.0.0",
"socket.io": "1.7.3",
"transform-loader": "0.2.4",
Expand Down
92 changes: 92 additions & 0 deletions packages/composer-tests-functional/scripts/http.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

const assert = require('assert');
const bodyParser = require('body-parser');
const express = require('express');
const fs = require('fs');
const http = require('http');

const app = express();
app.use(bodyParser.json());
const server = http.createServer(app);

app.use(function(req, res, next) {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept');
res.header('Access-Control-Allow-Methods', 'GET, HEAD, POST, PUT, DELETE, PATCH, OPTIONS');
next();
});

['GET', 'HEAD', 'POST', 'PUT', 'DELETE', 'PATCH'].forEach((method) => {
app[method.toLowerCase()]('/api/basic', (req, res) => {
res.status(200).json({
method: req.method
});
});
});

['GET', 'HEAD', 'POST', 'PUT', 'DELETE', 'PATCH'].forEach((method) => {
app[method.toLowerCase()]('/api/error', (req, res) => {
res.status(500).json({
method: req.method,
error: 'such error'
});
});
});

['GET', 'HEAD', 'POST', 'PUT', 'DELETE', 'PATCH'].forEach((method) => {
app[method.toLowerCase()]('/api/assetin', (req, res) => {
assert.deepStrictEqual(req.body, {
$class: 'systest.transactions.http.DummyAsset',
assetId: '1234',
integerValue: 12345678,
stringValue: 'hello world'
});
res.status(200).json({
method: req.method
});
});
});

['GET', 'HEAD', 'POST', 'PUT', 'DELETE', 'PATCH'].forEach((method) => {
app[method.toLowerCase()]('/api/assetout', (req, res) => {
res.status(200).json({
method: req.method,
asset: {
$class: 'systest.transactions.http.DummyAsset',
assetId: '1234',
integerValue: 12345678,
stringValue: 'hello world'
}
});
});
});

process.on('SIGINT', function () {
fs.unlinkSync('http.port');
});

(async () => {
await new Promise((resolve, reject) => {
server.on('error', reject);
server.on('listening', resolve);
server.listen();
});
const port = server.address().port;
fs.writeFileSync('http.port', port, { encoding: 'utf8' });
process.send('ready');
})();
11 changes: 9 additions & 2 deletions packages/composer-tests-functional/scripts/run-fv-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,9 @@ for FVTEST in $(echo ${FVTEST} | tr "," " "); do
npm publish --registry http://localhost:4873

if [ `uname` = "Darwin" ]; then
GATEWAY=docker.for.mac.localhost
export GATEWAY=docker.for.mac.localhost
else
GATEWAY="$(docker inspect hlfv1_default | grep Gateway | cut -d \" -f4)"
export GATEWAY="$(docker inspect hlfv1_default | grep Gateway | cut -d \" -f4)"
fi
echo registry=http://${GATEWAY}:4873 > /tmp/npmrc
cd "${DIR}"
Expand Down Expand Up @@ -131,9 +131,16 @@ for FVTEST in $(echo ${FVTEST} | tr "," " "); do
fi
fi

# Start all test programs.
npm run stop_http
npm run start_http

# Run the system tests.
npm run systest:${FVTEST} 2>&1 | tee

# Stop all test programs.
npm run stop_http

# Kill and remove any started Docker images.
if [ "${DOCKER_FILE}" != "" ]; then
ARCH=$ARCH docker-compose -f ${DOCKER_FILE} kill
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
namespace systest.transactions.http

transaction Basic {
o String method
}

transaction Error {
o String method
}

transaction AssetIn {
o String method
}

transaction AssetOut {
o String method
}

asset DummyAsset identified by assetId {
o String assetId
o String stringValue
o Integer integerValue
}
Loading