How can I mock requests and the response?

This is how you can do it (you can run this file as-is): import requests import unittest from unittest import mock # This is the class we want to test class MyGreatClass: def fetch_json(self, url): response = requests.get(url) return response.json() # This method will be used by the mock to replace requests.get def mocked_requests_get(*args, **kwargs): … Read more

Simultaneous Requests to PHP Script

The server, depending on its configuration, can generally serve hundreds of requests at the same time — if using Apache, the MaxClients configuration option is the one saying : The MaxClients directive sets the limit on the number of simultaneous requests that will be served. Any connection attempts over the MaxClients limit will normally be … Read more

How to use a servlet filter in Java to change an incoming servlet request url?

Implement javax.servlet.Filter. In doFilter() method, cast the incoming ServletRequest to HttpServletRequest. Use HttpServletRequest#getRequestURI() to grab the path. Use straightforward java.lang.String methods like substring(), split(), concat() and so on to extract the part of interest and compose the new path. Use either ServletRequest#getRequestDispatcher() and then RequestDispatcher#forward() to forward the request/response to the new URL (server-side redirect, … Read more

Persistent Service Worker in Chrome Extension

This is caused by these problems in ManifestV3: crbug.com/1024211, the worker doesn’t wake up for webRequest events. The workarounds are listed below. crbug.com/1271154, the worker is randomly broken after an update. Mostly fixed in Chrome 101. Per the service worker (SW) specification, it can’t be persistent and the browser must forcibly terminate all of SW … Read more

Removing nested promises

From every then callback, you will need to return the new promise: exports.viewFile = function(req, res) { var fileId = req.params.id; boxContentRequest(‘files/’ + fileId + ‘/content’, req.user.box.accessToken) .then(function(response) { return boxViewerRequest(‘documents’, {url: response.request.href}, ‘POST’); }) .then(function(response) { return boxViewerRequest(‘sessions’, {document_id: response.body.id}, ‘POST’); }) .then(function(response) { console.log(response); }); }; The promise that is returned by the … Read more

How do I send a POST request with PHP?

CURL-less method with PHP5: $url=”http://server.com/path”; $data = array(‘key1’ => ‘value1’, ‘key2’ => ‘value2’); // use key ‘http’ even if you send the request to https://… $options = array( ‘http’ => array( ‘header’ => “Content-type: application/x-www-form-urlencoded\r\n”, ‘method’ => ‘POST’, ‘content’ => http_build_query($data) ) ); $context = stream_context_create($options); $result = file_get_contents($url, false, $context); if ($result === FALSE) … Read more