Create reusable aiohttp.ClientSession for unittest - python-3.x

I have some integration tests that make a few web requests per test, using the aiohttp package.
In each integration test, I have the below line of code to create a persistent session variable:
async with aiohttp.ClientSession() as session:
..make web call 1
..make web call 2
This all works fine but I'd like to move the 'async with aiohttp.ClientSession() as session' out of the test case, and into the base class so that it becomes a variable that all test cases can access. Despite my best efforts,I can only get to a point where the first call in the test is made successfully, but then the session is closed, causing the second call to fail. Does anyone know how to setup the ClientSession(), perhaps in the setUp() declaration.
Ref: https://docs.aiohttp.org/en/latest/http_request_lifecycle.html

Related

create a persistant instance of aiohttp.ClientSession in Django app

I'm trying to create an instance of aiohttp.ClientSession() in a django app. I want to create the object via the ready() hook in apps.py and then access the instance from views.py.
From views.py I'm using the my_app_config.my_object to pull back the instance, but I'm seeing weird behaviour. For example 'Timeout context manager should be used in side a task'.
Is anyone able to provide a clear example of how I might go about creating an instance of aiohttp.ClientSession() which I can pull into views.py (or indeed any other class in the app) for reuse.

is there a way to run flask on a thread?

in order to implement a system which exposes REST-api and run another functionality simultaneously, i tried to use flask like the flask like the following:
app = Flask(__name__)
app.run(threaded=True)
foo()
but foo function never starts.
I would to understand how to solve the problem or get an alternative option to implement it.
Thanks!
Going to the documentation for Flask.run() we see that the options provided (such as threaded) are forwarded to the underlying function werkzeug.run_simple(). The documentation for werkzeug.run_simple() says the following about the threaded parameter:
threaded – should the process handle each request in a separate thread?
This means that each REST call will be handled in a separate thread, but does not start the server in the background (which seems to be what you want). Instead, you can use the Process class from multiprocessing:
from flask import Flask
app = Flask(__name__)
from multiprocessing import Process
p = Process(target=app.run)
p.start()
foo()
This will start your flask app in the background, letting you run other functions after starting the app.

using Locust.io for REST web service

I wanted to use Locust for performance testing on Spring Rest WebService, where each service is secured by the token.
is anyone tried to do the same by nesting task sets?
How can we maintain the same token for all the request from single user?
is it possible to go to the task on response from other task?
I had a similar scenario. If you know what the token is in advance, you can do:
def on_start(self):
""" on_start is called when a Locust starts, before any task is scheduled """
self.access_token = "XYZ" # method 1
# self.login() # <-- method 2
Otherwise you could call something like a login method that would authenticate your user, and then store the resulting token on self.
Since on start happens before any tasks, I never had to worry about nesting task sets.
If you need things to happen in a certain order within tasks, you can just run something like:
#task(1)
def mytasks(self):
self.get_service_1()
self.get_service_2()

SoapUI - force endpoint URL when using testrunner.bat

I'm trying to run a SoapUI test-suite against two different endpoints and I do this by triggering two testrunner command and supply two different "-e" argument values.
The problem is that each of my test cases uses one API that I am testing, for which I do need to use the endpoint that is being passed under -e argument, and another API that should remain static. (The 2nd API is a helper API which sets up the environment for the first API to be able to work). So if I use the -e argument it breaks my tests because it forces the 2nd API to the same endpoint as the first API.
What I've tried so far is using the following groovy script to force endpoint value for specific Test Steps, however it's being ignored or maybe the script runs before the endpoints gets set, I'm not sure.
TestSuite setup script:
def testCases = testSuite.getTestCaseList()
for(testCase in testCases)
{
def testSteps = testCase.getTestStepList()
for(testStep in testSteps)
{
if(testStep.name == "my name")
{
testStep.setPropertyValue('endpoint','http://force.it');
}
}
}
What else can I do to overcome this issue to avoid duplicating my tests?
If you're right seems that e argument overrides all endpoints inclusive the ones you set in the setup script.
Then I purpose the follow approach for your case. SOAPUI as probably you already know has properties at different levels (testSuite, testCase, project, global) and you can use this properties to share information between your tests.
The thing is that you can use this properties to set your endpoints and pass the properties values in the testrunner command.
Set the endpoint for all your test request which test your 1st API using a global property:
${#Project#endpointAPI1}
And for the 2nd API set the endpoint url as:
${#Project#endpointAPI2}
Note: If you don't want to set the endpoints one by one you can use a groovy script testStep similar to the one you show in your question.
Once this is set, then you can invoke the testrunner for your both cases using the properties:
Then to test your cases you can add the follow properties, with -P properties are added at project level.
First endpoint for api1
-PendpointAPI1=http://one_endpointAPI1.com -PendpointAPI2=http://endpointAPI2.com
Second endpoint for api1
-PendpointAPI1=http://second_endpointAPI1.com -PendpointAPI2=http://endpointAPI2.com
Note that I use also a variable for endpoint API2 however if this is static and not change between both tests instead of using ${#Project#endpointAPI2} you can set directly the url for this service and pass only the property -PendpointAPI1.
Hope it helps,

How to step into a OWIN middleware in a package?

I have the following stack in my ASPNET 5 app startup:
appBuilder.UseIdentityServerBearerTokenAuthentication();
// That calls
app.UseValidationEndpoint();
// That calls
app.UseOAuthBearerAuthentication();
// That calls
app.Use(typeof(OAuthBearerAuthenticationMiddleware), app, options);
The final call adds OAuthBearerAuthenticationMiddleware to the middleware pipeline, this class overrrides the CreateHandler() method returning a new instance of OAuthBearerAuthenticationHandler class.
OAuthBearerAuthenticationHandler class is the class that deals with the bearer token and that's where I'm trying to step into. The problem is that I can only set a breakpoint at the Startup.Configure() method, and this method runs only once when the application starts.
Even in this circutsnace I have trued to step into OAuthBearerAuthenticationHandler using the original Katana source code that I have downloaded from CodePlex and loading appropriate symbols but for some reason the symbols won't give me information about the OAuthBearerAuthenticationHandler class.
I really need to see what's going on inside that class, in special into the AuthenticateCoreAsync() method, but I don't know how to capture a request and go through the pipeline stack and get to that class as Startup.Configure() is only called once and in my circunstance I only have a problem when I provice a bearer token. I hope I was clear enough to get an answer :)

Resources