-
Notifications
You must be signed in to change notification settings - Fork 51
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
fix: use regex to avoid falsely replacing ports starting with 80 #124
Conversation
Codecov Report
@@ Coverage Diff @@
## dev #124 +/- ##
=======================================
Coverage 97.18% 97.18%
=======================================
Files 1 1
Lines 71 71
Branches 39 39
=======================================
Hits 69 69
Misses 2 2
Continue to review full report at Codecov.
|
I was hesitant at first, but I double checked and noticed the test cases includes a trailing slash to the baseURL here: Line 11 in c002aaa
So I added another commit that checks for this as well since my original regex will fail for baseURLs with trailing slash. Original Regex
New Regex
The |
Hi @azrikahar thanks for PR and nice explanations. I think we only have one issue with current regex which is when baseURL also has a path like ( |
@pi0 thank you for the review :) Can't believe that scenario slipped my mind 🤦♂️ From my understanding, Definitely not super experienced in regex so here's additional tests for you to review in case I still miss anything. Test codeClick to toggle source codeconst baseUrls = [
'http://foo.com:80',
'http://foo.com:8080',
'http://foo.com:80/',
'http://foo.com:8080/',
'http://foo.com:80/foo',
'http://foo.com:8080/foo',
]
const rg = /^http:\/\/.*:80(\/|$)/
baseUrls.forEach(baseUrl => {
console.log('before:', baseUrl)
console.log('after: ', rg.test(baseUrl) ? baseUrl.replace(':80', '') : baseUrl)
console.log('---')
}) Test result |
Thanks for investigation @azrikahar <3 I merged with |
This PR fixes #120.
Problem Statement
Following what the issue addresses, the following logic is currently used to remove
:80
when the baseURL starts withhttp://
.http/lib/module.js
Lines 117 to 120 in c002aaa
However, this logic will also be true for ANY ports that starts with
:80
, such as:8081
,:8000
and so on since they do technically contain:80
in them.PR Fix
We can use the regex
/^http:\/\/.*:80$/
to make the check stricter, which requires the baseURL to fulfill all the following conditions:http://
(^http:\/\/
:^
means "start of line" and we need to escape/
into\/
).*
:.
means any character and*
means appearing zero or more times):80
only (:80$
:$
means "end of line")Test Results
Test Case 1 - baseURL ends with :80
Both logic works as intended.
Test Case 2 - baseURL ends with :8081
Here we can see the original logic falsely turns
http://localhost:8081
tohttp://localhost81
(as reported by the issue) whereas the regex logic will not trigger as intended.