Skip to content

Commit

Permalink
Merge pull request #4 from TryxAPI/jan5-requirements
Browse files Browse the repository at this point in the history
Fixes tests.
  • Loading branch information
vbudhram committed Apr 30, 2015
2 parents e5dad10 + b06b1fa commit f032a12
Show file tree
Hide file tree
Showing 16 changed files with 876 additions and 876 deletions.
9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,27 +20,28 @@
},
"homepage": "https://github.com/TryxAPI/lrs-conformance-tests",
"dependencies": {
"chai": "<1.10.0",
"chai": "^1.9.2",
"chai-as-promised": "^4.1.1",
"commander": "^2.6.0",
"crypto": "0.0.3",
"dirty-chai": "^1.0.0",
"exit": "^0.1.2",
"extend": "^2.0.0",
"form-urlencoded": "0.0.7",
"joi": "^5.1.0",
"lodash-node": "^3.2.0",
"mocha": "^1.20.1",
"moment": "^2.8.4",
"node-env-file": "0.1.3",
"node-uuid": "^1.4.1",
"q": "^1.1.2",
"qs": "^2.3.3",
"querystring": "^0.2.0",
"request": "^2.37.0",
"should": "^4.0.4",
"string": "^3.1.0",
"super-request": "0.0.8",
"supertest": "^0.13.0",
"querystring": "^0.2.0",
"qs": "^2.3.3",
"form-urlencoded": "0.0.7",
"supertest-as-promised": "^1.0.0",
"valid-url": "^1.0.9"
}
Expand Down
210 changes: 210 additions & 0 deletions test/multipartParser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,210 @@
(function (module, S) {
'use strict';

var headerParts = [
{
field: 'contentType',
fn: function (part) {
var s = S(part.toLowerCase());

var match;
if (s.startsWith('content-type:')) {
var type = part.substring('content-type:'.length).trim();
var regExp = new RegExp('\\w+\/[\\w.\\-\\+]+');
var matches = regExp.exec(type);
if (Array.isArray(matches)) {
match = matches[0];
}
}
return match;
}
},
{
field: 'boundary',
fn: function (part) {
var s = S(part);

var match;
if (s.contains('boundary=')) {
var index = s.toString().indexOf('boundary=');
var indexSemicolon = s.toString().indexOf(';', index);
var endIndex = (indexSemicolon < 0 ? s.toString().length : indexSemicolon);
match = part.substring(index + 'boundary='.length, endIndex);
}
return match;
}
},
{
field: 'contentTransferEncoding',
fn: function (part) {
var s = S(part.toLowerCase());

var match;
if (s.startsWith('content-transfer-encoding:')) {
match = part.substring('content-transfer-encoding:'.length).trim();
}
return match;
}
},
{
field: 'contentDisposition',
fn: function (part) {
var s = S(part.toLowerCase());

var match;
if (s.startsWith('content-disposition:')) {
var type = part.substring('content-disposition:'.length).trim();
var regExp = new RegExp('\\w+');
var matches = regExp.exec(type);
if (Array.isArray(matches)) {
match = matches[0];
}
}
return match;
}
},
{
field: 'filename',
fn: function (part) {
var s = S(part.toLowerCase());

var match;
if (s.startsWith('content-disposition:') && s.contains('filename="')) {
var index = s.toString().indexOf('filename="');
var filename = part.substring(index + 'filename="'.length);

var regExp = new RegExp('[\\w\\W+\\w+][^"]+');
var matches = regExp.exec(filename);
if (Array.isArray(matches)) {
match = matches[0];
}
}
return match;
}
}
];

function findDelimiter(content) {
var delimiter; // Delimiter for Windows, Linux, Mac
if (S(content).startsWith('\r\n')) {
delimiter = '\r\n';
} else if (S(content).startsWith('\r')) {
delimiter = '\r';
} else if (S(content).startsWith('\n')) {
delimiter = '\n';
} else {
throw new Error('Multipart: unknown delimiter.');
}
return delimiter;
}

function parsePart(delimiter, content) {
var parsed = {};
parsed.header = parseHeader(delimiter, content);

var index = content.indexOf(delimiter + delimiter);
parsed.body = content.substring(index + (delimiter.length * 2), content.length - delimiter.length);
return parsed;
}

function parseHeader(delimiter, content) {
var regExp = new RegExp(delimiter);
var parts = content.split(regExp);

var header = {parts: []};
if (parts.length < 2) {
throw new Error('Multipart: cannot parse header with invalid length.');
} else if (!S(parts[0]).isEmpty()) {
throw new Error('Multipart: cannot parse header with invalid value.');
} else if (S(parts[0]).isEmpty() && S(parts[1]).isEmpty()) {
header.parts.push('Content-Type: text/plain');
header.contentType = 'text/plain';
return header;
}

for (var i = 1; i < parts.length; i++) {
var part = parts[i];
if (S(part).isEmpty()) {
return header;
} else {
parseHeaderParts(header, part);
header.parts.push(part);
}
}
return header;
}

function parseHeaderParts(header, part) {
headerParts.forEach(function (one) {
var value = one.fn(part);
if (value) {
header[one.field] = value;
}
});
}


/**
* Searches in string to find boundary.
* @param {String} string - String to search
* @return {String}
*/
module.exports.getBoundary = function getBoundary(string) {
var boundary = '';

for (var i = 0; i < headerParts.length; i++) {
var header = headerParts[i];

if (header.field === 'boundary') {
boundary = header.fn(string);
break;
}
}
return boundary;
};

/**
* Parses multipart/mixed content (http://www.w3.org/Protocols/rfc1341/7_2_Multipart.html). This does not parse streams.
*
* {
* header; {
* parts: ['each header line since not all are mapped'],
* contentType: 'extracted Content-Type',
* contentTransferEncoding: 'extracted Content-Transfer-Encoding',
* contentDisposition: 'extracted Content-Disposition',
* filename: 'extracted filename'
* }
* body: 'extracted body from part'
* }
*
* @param {String} boundary - Boundary defined in header
* @param {String} body - Request body
*/
module.exports.parseMultipart = function parseMultipart(boundary, body) {
var dashedBoundary = '--' + boundary;
var index = body.indexOf(dashedBoundary);
if (index < 0) {
throw new Error('Multipart: boundary not found.');
}

var delimiter = findDelimiter(body.substring(index + dashedBoundary.length));

var regExp = new RegExp('--*' + boundary);
var contents = body.split(regExp);

var parts = [];
var lastBoundary = false;
for (var i = 1; i < contents.length; i++) {
var content = contents[i];

if (S(content).startsWith('--' + delimiter) || S(content).startsWith('--')) {
lastBoundary = true;
}

if (!lastBoundary) {
parts.push(parsePart(delimiter, content));
}
}
return parts;
};
}(module, require('string')));
36 changes: 19 additions & 17 deletions test/v1_0_2/configs/activities.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
// defines overwriting data
var INVALID_IRI = 'abc://should.fail.com';
var INVALID_NUMERIC = 12345;
var INVALID_OBJECT = {key: 'value'};
var INVALID_OBJECT = {test: 'value'};
var INVALID_STRING = 'should error';
var INVALID_INTERACTION_COMPONENT_ID = {
'id': INVALID_OBJECT,
Expand Down Expand Up @@ -48,10 +48,12 @@
];
var VALID_ACTIVITY = {id: 'http://www.example.com/meetings/occurances/34534'};
var VALID_EXTENSIONS = {
'http://example.com/profiles/meetings/extension/location': 'X:\\meetings\\minutes\\examplemeeting.one',
'http://example.com/profiles/meetings/extension/reporter': {
'name': 'Thomas',
'id': 'http://openid.com/342'
extensions: {
'http://example.com/profiles/meetings/extension/location': 'X:\\meetings\\minutes\\examplemeeting.one',
'http://example.com/profiles/meetings/extension/reporter': {
'name': 'Thomas',
'id': 'http://openid.com/342'
}
}
};
var VALID_INTERACTION_COMPONENT = {
Expand Down Expand Up @@ -1621,7 +1623,7 @@
name: 'statement activity "choice choices" missing "id"',
templates: [
{statement: '{{statements.object_activity}}'},
{object: '{{activities.choice}}'},
{object: '{{activities.choice_no_choices}}'},
{definition: {choices: [INVALID_INTERACTION_NO_ID]}}
],
expect: [400]
Expand All @@ -1630,7 +1632,7 @@
name: 'statement activity "likert scale" missing "id"',
templates: [
{statement: '{{statements.object_activity}}'},
{object: '{{activities.likert}}'},
{object: '{{activities.likert_no_scale}}'},
{definition: {scale: [INVALID_INTERACTION_NO_ID]}}
],
expect: [400]
Expand All @@ -1639,7 +1641,7 @@
name: 'statement activity "matching source" missing "id"',
templates: [
{statement: '{{statements.object_activity}}'},
{object: '{{activities.matching}}'},
{object: '{{activities.matching_no_source}}'},
{definition: {source: [INVALID_INTERACTION_NO_ID]}}
],
expect: [400]
Expand All @@ -1648,7 +1650,7 @@
name: 'statement activity "matching target" missing "id"',
templates: [
{statement: '{{statements.object_activity}}'},
{object: '{{activities.matching}}'},
{object: '{{activities.matching_no_target}}'},
{definition: {target: [INVALID_INTERACTION_NO_ID]}}
],
expect: [400]
Expand All @@ -1657,7 +1659,7 @@
name: 'statement activity "performance steps" missing "id"',
templates: [
{statement: '{{statements.object_activity}}'},
{object: '{{activities.performance}}'},
{object: '{{activities.performance_no_steps}}'},
{definition: {steps: [INVALID_INTERACTION_NO_ID]}}
],
expect: [400]
Expand All @@ -1666,7 +1668,7 @@
name: 'statement activity "sequencing choices" missing "id"',
templates: [
{statement: '{{statements.object_activity}}'},
{object: '{{activities.sequencing}}'},
{object: '{{activities.sequencing_no_choices}}'},
{definition: {choices: [INVALID_INTERACTION_NO_ID]}}
],
expect: [400]
Expand All @@ -1676,7 +1678,7 @@
templates: [
{statement: '{{statements.object_substatement}}'},
{object: '{{substatements.activity}}'},
{object: '{{activities.choice}}'},
{object: '{{activities.choice_no_choices}}'},
{definition: {choices: [INVALID_INTERACTION_NO_ID]}}
],
expect: [400]
Expand All @@ -1686,7 +1688,7 @@
templates: [
{statement: '{{statements.object_substatement}}'},
{object: '{{substatements.activity}}'},
{object: '{{activities.likert}}'},
{object: '{{activities.likert_no_scale}}'},
{definition: {scale: [INVALID_INTERACTION_NO_ID]}}
],
expect: [400]
Expand All @@ -1696,7 +1698,7 @@
templates: [
{statement: '{{statements.object_substatement}}'},
{object: '{{substatements.activity}}'},
{object: '{{activities.matching}}'},
{object: '{{activities.matching_no_source}}'},
{definition: {source: [INVALID_INTERACTION_NO_ID]}}
],
expect: [400]
Expand All @@ -1706,7 +1708,7 @@
templates: [
{statement: '{{statements.object_substatement}}'},
{object: '{{substatements.activity}}'},
{object: '{{activities.matching}}'},
{object: '{{activities.matching_no_target}}'},
{definition: {target: [INVALID_INTERACTION_NO_ID]}}
],
expect: [400]
Expand All @@ -1716,7 +1718,7 @@
templates: [
{statement: '{{statements.object_substatement}}'},
{object: '{{substatements.activity}}'},
{object: '{{activities.performance}}'},
{object: '{{activities.performance_no_steps}}'},
{definition: {steps: [INVALID_INTERACTION_NO_ID]}}
],
expect: [400]
Expand All @@ -1726,7 +1728,7 @@
templates: [
{statement: '{{statements.object_substatement}}'},
{object: '{{substatements.activity}}'},
{object: '{{activities.sequencing}}'},
{object: '{{activities.sequencing_no_choices}}'},
{definition: {choices: [INVALID_INTERACTION_NO_ID]}}
],
expect: [400]
Expand Down
Loading

0 comments on commit f032a12

Please sign in to comment.