Fix application type handling for urls with parameters
MODIFY
lib/basedriver/helpers.js
lib/basedriver/helpers.js
@@ -22,6 +22,7 @@ const APPLICATIONS_CACHE = new LRU({
});
const APPLICATIONS_CACHE_GUARD = new AsyncLock();
const SANITIZE_REPLACEMENT = '-';
+const DEFAULT_BASENAME = 'appium-app';
async function retrieveHeaders (link) {
try {
@@ -73,7 +74,7 @@ async function configureApp (app, supportedAppExtensions) {
let shouldUnzipApp = false;
let archiveHash = null;
let currentModified = null;
- const {protocol} = url.parse(newApp);
+ const {protocol, pathname} = url.parse(newApp);
const isUrl = ['http:', 'https:'].includes(protocol);
return await APPLICATIONS_CACHE_GUARD.acquire(app, async () => {
@@ -96,12 +97,14 @@ async function configureApp (app, supportedAppExtensions) {
}
let fileName = null;
- // to determine if we need to unzip the app, we have a number of places
- // to look: content type, content disposition, or the file extension
- if (ZIP_EXTS.includes(path.extname(newApp))) {
- fileName = sanitize(path.basename(newApp), {
+ const basename = sanitize(path.basename(decodeURIComponent(pathname)), {
replacement: SANITIZE_REPLACEMENT
});
+ const extname = path.extname(basename);
+ // to determine if we need to unzip the app, we have a number of places
+ // to look: content type, content disposition, or the file extension
+ if (ZIP_EXTS.includes(extname)) {
+ fileName = basename;
shouldUnzipApp = true;
}
if (headers['content-type']) {
@@ -109,15 +112,15 @@ async function configureApp (app, supportedAppExtensions) {
// the filetype may not be obvious for certain urls, so check the mime type too
if (ZIP_MIME_TYPES.includes(headers['content-type'])) {
if (!fileName) {
- fileName = 'appium-app.zip';
+ fileName = `${DEFAULT_BASENAME}.zip`;
}
shouldUnzipApp = true;
}
}
if (headers['content-disposition'] && /^attachment/i.test(headers['content-disposition'])) {
+ logger.debug(`Content-Disposition: ${headers['content-disposition']}`);
const match = /filename="([^"]+)/i.exec(headers['content-disposition']);
if (match) {
- logger.debug(`Parsed file name '${match[1]}' from 'Content-Disposition' header`);
fileName = sanitize(match[1], {
replacement: SANITIZE_REPLACEMENT
});
@@ -125,8 +128,17 @@ async function configureApp (app, supportedAppExtensions) {
}
}
if (!fileName) {
- // assign the default extension if none has been detected
- fileName = `appium-app${_.first(supportedAppExtensions)}`;
+ // assign the default file name and the extension if none has been detected
+ const resultingName = basename
+ ? basename.substring(0, basename.length - extname.length)
+ : DEFAULT_BASENAME;
+ let resultingExt = extname;
+ if (!supportedAppExtensions.includes(resultingExt)) {
+ logger.info(`The current file extension '${resultingExt}' is not supported. ` +
+ `Defaulting to '${_.first(supportedAppExtensions)}'`);
+ resultingExt = _.first(supportedAppExtensions);
+ }
+ fileName = `${resultingName}${resultingExt}`;
}
const targetPath = await tempDir.path({
prefix: fileName,

Fix application type handling for urls with parameters (#303)