forked from jlidder/jswiremock
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathUrlParser.js
120 lines (109 loc) · 3.84 KB
/
UrlParser.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/**
* Created by jlidder on 15-07-18.
*/
function UrlNode(data) {
this.append = function (next) {
this.next = next;
};
this.getData = function () {
return this.data;
};
this.setData = function (data) {
this.data = data;
}
this.getNext = function () {
return this.next;
};
this.setParams = function (params) {
var queryParameters = params.split("&");
if (queryParameters != null) {
this.params = [];
for (var counter = 0; counter < queryParameters.length; counter++) {
var keyValuePair = queryParameters[counter].split("=");
this.params[keyValuePair[0]] = keyValuePair[1];
}
} else {
queryParameters = null;
}
};
this.getParams = function () {
return this.params;
};
//set var's for this object
var queryParamSplit = data.split("?");
this.params = null;
this.data = null;
this.next = null;
if (queryParamSplit != null && queryParamSplit.length > 1) {
this.setData(queryParamSplit[0]);
this.setParams(queryParamSplit[1]);
} else {
this.setData(data);
}
};
exports.buildUrlStorageLinkedList = function (url) {
var receivedSplitUrl = url.split("/");
var parentNode = null;
var currentNode = null;
for (var counter = 0; counter < receivedSplitUrl.length; counter++) {
if (counter == 0) {
if (receivedSplitUrl[counter] === "") { //edge case when '/1/ url, then split function returns empty first element
counter++;
}
parentNode = new UrlNode(receivedSplitUrl[counter]);
currentNode = parentNode;
} else {
var newNode = new UrlNode(receivedSplitUrl[counter])
currentNode.append(newNode);
currentNode = newNode;
}
}
return parentNode;
};
exports.hasMatchingStub = function (receivedUrlPartsLinkedList, getRequestStubs) {
var matchStubs = [];
for (var counter = 0; counter < getRequestStubs.length; counter++) {
if (recursiveUrlLinkedListSearch(receivedUrlPartsLinkedList, getRequestStubs[counter].getUrl()) == true) {
matchStubs.push(getRequestStubs[counter]);
}
}
return matchStubs;
};
function recursiveUrlLinkedListSearch(receiveUrlParts, storedUrlParts) {
//base case
// if receiveUrlParts is empty linked list, then RETURN TRUE end is arrived
if (receiveUrlParts == null) {
return true;
}
//error scenario, we return 0
if (storedUrlParts == null && receiveUrlParts != null) {
return false;
}
if (storedUrlParts == null && receiveUrlParts == null) {
return true;
}
//recursive case
// if first element in receiveUrlParts is equal to first node in storedUrlParts, then CONTINUE
// else RETURN FALSE
if (storedUrlParts.getData().match(/(\:\w+)/) != null ||
receiveUrlParts.getData() === storedUrlParts.getData()) {
if (receiveUrlParts.getNext() == null && storedUrlParts.getNext() == null && receiveUrlParts.getParams() != null) { //check if next query params are equal
return checkEqualQueryParams(receiveUrlParts.getParams(), storedUrlParts.getParams());
}
return recursiveUrlLinkedListSearch(receiveUrlParts.getNext(), storedUrlParts.getNext());
} else {
return false;
}
};
function checkEqualQueryParams(receivedQueryParams, storedQueryParams) {
for (key in receivedQueryParams) {
if (storedQueryParams && storedQueryParams[key] != null) { //check if key exists in stored query params
if (storedQueryParams[key] === receivedQueryParams[key] || receivedQueryParams[key].match(/(\:\w+)/) != null) {
return true;
}
} else {
return false;
}
}
return true;
}