Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

reworked a way of request url parts parsing #111

Merged
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
2 changes: 1 addition & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"expect": true
},
"parserOptions": {
"ecmaVersion": 2018
"ecmaVersion": 2020
},
"rules": {
"node/no-deprecated-api": "off",
Expand Down
2 changes: 1 addition & 1 deletion .jshintrc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"node": true,
"esversion": 6
"esversion": 11
}
19 changes: 13 additions & 6 deletions fiftyone.pipeline.core/evidence.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,15 +148,22 @@ class Evidence {

evidence.add('server.host-ip', request.connection.localAddress.toString());

// Get querystring data
// Parsing URL using new URL constructor

const protocol = request.protocol || 'http';
const hostname = request.hostname || 'localhost'; // Use a default hostname if not available
const port = request.socket?.localPort || ''; // Port may not be available in some cases

const params = new url.URL(request.url);
// If request.url already contains the protocol, hostname, and port, use it as is
const fullURL = request.url.includes('://') ? request.url : `${protocol}://${hostname}${port ? `:${port}` : ''}${request.url}`;

// Get querystring data

const query = params.searchParams;
const URL = new url.URL(fullURL);
const query = URL.searchParams;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we have some tests directly in this package that would check that the evidence is correctly parsed from query params?


Object.keys(query).forEach(function (key) {
const value = query[key];
evidence.add('query.' + key, value);
query.forEach(function (value, param) {
evidence.add('query.' + param, value);
});

return this;
Expand Down
62 changes: 62 additions & 0 deletions fiftyone.pipeline.core/tests/evidence.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,65 @@ test('evidenceKeyFilter', () => {
)
.toBe('header.user_agent');
});

/**
* Check that evidence addFromRequest
* can correctly handle Incoming Request object */

test('evidence addFromRequest partial url', () => {
const { EventEmitter } = require('events');

// Mock request object
const mockRequest = new EventEmitter();
mockRequest.method = 'POST'; // Example HTTP method
mockRequest.url = '/?some-value=some'; // Example request URL
mockRequest.httpVersion = '1.1'; // Example HTTP version
mockRequest.hostname = 'test.url';
mockRequest.headers = {
'Content-Type': 'application/json',
'Content-Length': 18
}; // Example request headers

// Simulating request body data
const mockBodyData = JSON.stringify({ evidence: 'sample' });
mockRequest.emit('data', mockBodyData);
mockRequest.emit('end');

// Simulating connection object
mockRequest.connection = {
remoteAddress: '127.0.0.1',
localAddress: '127.0.0.1'
};

expect(() => syncFlowData.evidence.addFromRequest(mockRequest)).not.toThrow();
expect(syncFlowData.evidence.get('query.some-value')).toBe('some');
});

test('evidence addFromRequest full url', () => {
const { EventEmitter } = require('events');

// Mock request object
const mockRequest = new EventEmitter();
mockRequest.method = 'POST'; // Example HTTP method
mockRequest.url = 'http://test.com/path?some-value=some'; // Example request URL
mockRequest.httpVersion = '1.1'; // Example HTTP version
mockRequest.hostname = 'test.url';
mockRequest.headers = {
'Content-Type': 'application/json',
'Content-Length': 18
}; // Example request headers

// Simulating request body data
const mockBodyData = JSON.stringify({ evidence: 'sample' });
mockRequest.emit('data', mockBodyData);
mockRequest.emit('end');

// Simulating connection object
mockRequest.connection = {
remoteAddress: '127.0.0.1',
localAddress: '127.0.0.1'
};

expect(() => syncFlowData.evidence.addFromRequest(mockRequest)).not.toThrow();
expect(syncFlowData.evidence.get('query.some-value')).toBe('some');
});
Loading