How to use fixPythonContentType method in Appium Base Driver

Best JavaScript code snippet using appium-base-driver

Run Appium Base Driver automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

middleware.js

Source: middleware.js Github

copy
1import log from './logger';
2import { errors } from 'mobile-json-wire-protocol';
3
4
5function allowCrossDomain (req, res, next) {
6  try {
7    res.header('Access-Control-Allow-Origin', '*');
8    res.header('Access-Control-Allow-Methods', 'GET,POST,PUT,OPTIONS,DELETE');
9    res.header('Access-Control-Allow-Headers', 'origin, content-type, accept');
10
11    // need to respond 200 to OPTIONS
12    if ('OPTIONS' === req.method) {
13      res.sendStatus(200);
14    } else {
15      next();
16    }
17  } catch (err) {
18    log.error(`Unexpected error: ${err.stack}`);
19    next();
20  }
21}
22
23function fixPythonContentType (req, res, next) {
24  // hack because python client library gives us wrong content-type
25  if (/^\/wd/.test(req.path) && /^Python/.test(req.headers['user-agent'])) {
26    if (req.headers['content-type'] === 'application/x-www-form-urlencoded') {
27      req.headers['content-type'] = 'application/json';
28    }
29  }
30  next();
31}
32
33function catchAllHandler (err, req, res, next) {
34  log.error(`Uncaught error: ${err.message}`);
35  log.error('Sending generic error response');
36  try {
37    res.status(500).send({
38      status: errors.UnknownError.code(),
39      value: `ERROR running Appium command: ${err.message}`
40    });
41    log.error(err);
42  } catch (ign) {
43    next(ign);
44  }
45}
46
47function catch4XXHandler (err, req, res, next) {
48  if (err.status >= 400 && err.status < 500) {
49    // set the content type to `text/plain`
50    // https://code.google.com/p/selenium/wiki/JsonWireProtocol#Responses
51    log.debug(`Setting content type to 'text/plain' for HTTP status '${err.status}'`);
52    res.set('Content-Type', 'text/plain');
53    res.status(err.status).send(`Unable to process request: ${err.message}`);
54  } else {
55    next(err);
56  }
57}
58
59function catch404Handler (req, res) {
60  // set the content type to `text/plain`
61  // https://code.google.com/p/selenium/wiki/JsonWireProtocol#Responses
62  log.debug('No route found. Setting content type to \'text/plain\'');
63  res.set('Content-Type', 'text/plain');
64  res.status(404).send(`The URL '${req.originalUrl}' did not map to a valid resource`);
65}
66
67export { allowCrossDomain, fixPythonContentType, catchAllHandler, catch404Handler, catch4XXHandler };
68
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Run JavaScript Tests on LambdaTest Cloud Grid

Execute automation tests with Appium Base Driver on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)