Skip to content

Commit

Permalink
Resolving path inconsistency in container.js and appium.js (#4866)
Browse files Browse the repository at this point in the history
* Update Appium.js

Fixed #4865
Enhancement: Prevent Double Slashes in Appium Endpoint URL
Overview:
This update improves the _buildAppiumEndpoint() function by ensuring the path does not end with a trailing slash. This prevents potential issues with double slashes when constructing the Appium REST API endpoint URL.

Changes:
Introduced normalizedPath, which removes a trailing slash from path using .replace(/\/$/, '').
Updated the return statement to use normalizedPath instead of path.
Benefits:
✅ Prevents malformed URLs with double slashes.
✅ Improves consistency and reliability of API requests.
✅ Enhances code readability and maintainability.

* Update container.js

Fixed #4863

* Update lib/container.js

Co-authored-by: kobenguyent <[email protected]>

---------

Co-authored-by: kobenguyent <[email protected]>
  • Loading branch information
mjalav and kobenguyent authored Mar 5, 2025
1 parent c313ef4 commit f3cd36e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
15 changes: 14 additions & 1 deletion lib/container.js
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,7 @@ function loadGherkinSteps(paths) {
loadSupportObject(path, `Step Definition from ${path}`)
}
} else {
const folderPath = paths.startsWith('.') ? path.join(global.codecept_dir, paths) : ''
const folderPath = paths.startsWith('.') ? normalizeAndJoin(global.codecept_dir, paths) : ''
if (folderPath !== '') {
globSync(folderPath).forEach(file => {
loadSupportObject(file, `Step Definition from ${file}`)
Expand Down Expand Up @@ -562,3 +562,16 @@ function getHelperModuleName(helperName, config) {
// built-in helpers
return `./helper/${helperName}`
}
function normalizeAndJoin(basePath, subPath) {
// Normalize and convert slashes to forward slashes in one step
const normalizedBase = path.posix.normalize(basePath.replace(/\\/g, '/'))
const normalizedSub = path.posix.normalize(subPath.replace(/\\/g, '/'))

// If subPath is absolute (starts with "/"), return it as the final path
if (normalizedSub.startsWith('/')) {
return normalizedSub
}

// Join the paths using POSIX-style
return path.posix.join(normalizedBase, normalizedSub)
}
4 changes: 3 additions & 1 deletion lib/helper/Appium.js
Original file line number Diff line number Diff line change
Expand Up @@ -383,8 +383,10 @@ class Appium extends Webdriver {

_buildAppiumEndpoint() {
const { protocol, port, hostname, path } = this.browser.options
// Ensure path does NOT end with a slash to prevent double slashes
const normalizedPath = path.replace(/\/$/, '');
// Build path to Appium REST API endpoint
return `${protocol}://${hostname}:${port}${path}/session/${this.browser.sessionId}`
return `${protocol}://${hostname}:${port}${normalizedPath}/session/${this.browser.sessionId}`
}

/**
Expand Down

0 comments on commit f3cd36e

Please sign in to comment.