Electron: mocking out network requests for tests - unit-testing

I'd like to write unit tests for some of my network logic in an Electron app, and need to mock out the network endpoints.
In the past, I've used nock for mocking out HTTP requests in Node. However, my Electron code uses the electron.net module for requests (instead of Node's http module), and Nock doesn't intercept these requests.
What is the best way to mock Electron network requests?

You can easily mock your API with Postman:
https://learning.postman.com/docs/postman/mock-servers/setting-up-mock/

I highly recommend this simple, platform independent, and open source tool for local testing:
https://www.electronjs.org/apps/local-mock-server
This is much simpler than paid-for options like postman, but I'm guessing the author would be very amenable to enhancements if you propose a PR here:
https://github.com/jayakumarreddy/Local-Mock-Server/
The configuration simply lives in your home directory (~/.config/local-mock-server/mocks_folder/ on Linux), so what I do is simply check these mocks into my git repo and symlink them there, that way everything I do is repeatable for someone else. Just double click the AppImage (on Linux) to enable the mock and leave it minimized for as long as needed. Done.

Related

Service or container that can Mock the Google Drive API?

I write a lot of apps that end up integrating with Google Drive for one reason or another. It's such a useful cloud based storage utility that it's got integrations all over the place.
So many of my integration tests are needing to call the actual Google API. Such a waste.
You can use Mockito to mock it, but that's a ton of work. And it's only for unit testing that has stubbed out mocks.
Has anyone made a mock HTTP web service that can mock the Google drive api?
I was hoping to find someone who created a Docker container that fires up a mock Google drive api that you can point the Google-drive-sdk to and have it work from a linux filesystem backend.
S3 has this. Example: https://hub.docker.com/r/lphoward/fake-s3/
I'm pretty sure no such docker container exists. Is there there anything in the makes at Google? Thanks for your time!
Here's what I have found:
For JAVA:
Please try checking HTTP Unit Testing. As mentioned in the documentation:
When writing unit tests using this HTTP framework, don't make requests to a real server. Instead, mock the HTTP transport and inject fake HTTP requests and responses. The pluggable HTTP transport layer of the Google HTTP Client Library for Java makes this flexible and simple to do.
Also, some useful testing utilities are included in the com.google.api.client.testing.http package (#Beta).
For Android:
If you're using Android Studio, the location of your test code depends on the type of test you are writing. Android Studio provides source code directories (source sets).
Local unit tests
Located at module-name/src/test/java/.
These are tests that run on your machine's local Java Virtual Machine (JVM). Use these tests to minimize execution time when your tests have no Android framework dependencies or when you can mock the Android framework dependencies.
At runtime, these tests are executed against a modified version of android.jar where all final modifiers have been stripped off. This lets you use popular mocking libraries, like Mockito.
You may want check this SO post and see if it helps.
For Python:
You may try Mocks.
The use of Mock objects is a standard testing methodology for Python and other object-oriented languages. This library defines Mock classes that simulate responses to API calls. You can use them to test how your code handles basic interactions with Google APIs.
You can use the HttpMock class which simulates the response to a single HTTP request.
Hope that helps!
For python, if you are brave, you could use vcrpy as gcsfs does. It records all requests and responses over HTTP into YAML files, which you can then test against, with complex matching (so that, for instance, etags and timestamps don't matter) and replacement rules (so that secure data isn't leaked into version control).

How to mock SOAP web service without SoapUI

I have a web service that I want to mock in following way: I will have a list of given IDs, and a set of response items for them, and if user will send a request with ID from the list, proper response should be sent back.
How to do it without tools like SoapUI (I don't want to install any additional software on the server that will be tested if possible).
Thanks in advance for any help.
SoapUI open source provides exactly what you want to achieve, without any need to install SoapUI on the server.
I consider this approach very efficient:
Create your mock service inside SoapUI.
Test the mock on your computer with SoapUI.
Create a WAR with the mock service (or more services) - just click on the project and choose "Deploy As WAR"
Deploy the WAR to the target server.
The resulting WAR is standalone and you do not need to deploy any other software.
I recommend this tutorial: https://www.soapui.org/soap-mocking/getting-started.html
Regards,
Karel
The easiest way I could find is https://www.mockable.io/ . Hope it helps.
You might have to build an actual mock for this.
This could range from just a different implementation for an existing interface (say IOrderQuerier, with the old OrderQuerier and the new MockOrderQuerier), to a different project altogether (say MockOrderApi).
In both scenarios, the Mock would just return a set of predefined values depending on the input, but you'll need to provide some sort of switch mechanism (for example a flag in the config file, which is read by the DI container).
You'll have to provide more details about the server if you need more targeted answers on this.
If you can manage to mock this using mockito, I've just added a simple project which does most of the heavy lifting: mockito-soap-cxf

Can a web service client with identical signature call a different web service?

My project has gotten into web services lately, but our QA team has a request to be able to set up a clone client and clone web service so that they can issue a request from the clone client to the real web service and from the real client to the clone service to test the real pieces individually.
The clone client to real web service part is easy, I just wrap the generated client in a main() app that picks up data to send that the test team specifies. But the service part is confusing to me. How can I make a dummy service that just echoes out its requests to a log without affecting the real service? I want the real client to use the same generated client code, just point the soft-coded URL to the URL of the dummy.
If I define a new web service with identical names and signatures to the real service, can the real client hit the dummy with just a URL change? Or is it more complicated? Am I barking up the wrong tree?
Re: Making a dummy service
This would actually be called making a "mock" service or a web service "stub". You don't even technically have to code it - you can use tools. For example, SoapUI has the ability to import a WSDL and create a mock service. The have some other discussion of mock services as well - they can be used both to test the client application (or to simply develop it before the back-end resources are ready or available). To use a WSDL-based service stub you would simply change the endpoint of your client-under-test to the stub instance. SoapUI will show you (and let you configure) the stub's endpoint URL.
The trendy term for service mocking and stubbing is "service virtualization" (not to be confused with server virtualization as in virtual machines). Searches using this terminology will find you even more powerful (and expensive) tooling.
Re: Just a URL Change
Yep, for the most part. So long as the XML namespaces are the same and the WSDL is the same (minus endpoint URL) you should be fine. Since the WSDL acts as an interface contract, the stub MUST accept those same inputs and produce those same outputs. How it does so, of course, is up to you.
PS - free extra advice - Don't code a dummy client. Use tools (I'll use SoapUI again as an example free tool) to make your web service testing more robust and repeatable. You can create test suites with sample SOAP requests, add assertions on how the service should behave, and best of all - your investment in time of creating a test project can be shared across team members, cutting down time to test setup and making sure testing is thorough and repeatable. If you code a client, every tester/user of the test client will have their own way of testing, response inspection will probably be manual, and you'll notice when your best QA tester goes on vacation because regressions slip into the web service product. Repeatable testing can nix this, and tool based automated functional testing is the bees knees. You're already on the right path by testing the pieces individually.

Fake http server for testing purpose

I have an application that connect to an IP camera and that do HTTP request to get image and M-JPEG. For now it's ok because I have the camera in the developpement room. But in a few weeks, the cam will be out in production (they are very expensive cam) so I will not have them for testing and debugging my app.
What I want to know is what would be the best way to "mock" these cams? For now, my application is using 2 cams, let's say they are on http://192.168.88.1 and http://192.168.88.2. I have think of this:
In my application, I could encapsulate the functions that do the
http calls in a class that could do real http request when in
release and fake when in debug. The bad side of this is that it will
not reproduce real thing like occasional timeout and network
traffic.
Maybe there is a ip cam simulator somewhere that I can put on my
network and use?
Maybe create real http request to a real server that I will have
programmed to return fake picture? If so, how to procede?
Like you see, I have ideas but I'm not sure what's the best and what people out there are doing. With your answers, take in consideration that I might use it for unit testing as well as normal debugging while the camera is away.
Thanks!
Ad. 1: This is a good approach when unit testing - your code should not depend on external servers/devices/file systems. But you already see that this environment might be too idealized. However I would aim to externalize HTTP infrastructure code - this will both improve your testing capabilities and overall architecture.
Ad. 2: Never heard of such, sorry.
Ad. 3: I think this is the most feasible solution. If you've ever worked with servlets, just grab Tomcat or Jetty and write one. Then connect to your servlet under localhost:8080/war_name/servlet_name and return whatever you want from it. Here is a dead simple example.
If you've never heard about Java servlets and servlet containers - it's still worth to learn about them. But it might be faster to start an HTTP server built into the Sun JDK, see HttpServer API and an example.
If you want real images and have access to a webcam, you could create, in your solution, an ASP.Net webservice that takes a picture out of your webcam on request.
When you're in debug mode, configure your solution to start the webservice on the asp.net developpement server, so you don't have to configure a "real" web server
Here is a link to code to take a snapshot from c#
It's for winforms, but it should be adaptable for a web service
http://sites.google.com/site/webcamlibrarydotnet/winfrom-and-csharp-sample-code-and-download
I'd recommend creating a simple test harness using Sinatra. Sinatra is very simple to use to stub out any kind of basic web app like this.
For example, here's a real program written using Sinatra:
require 'sinatra'
get '/hi' do
"Hello World!"
end
This will run a simple web listener that will respond Hello World! if you hit the URI /hi.
Sinatra is a Ruby-based app and it's best if you know Ruby. If you don't or if you have a strong preference for another language, Sinatra's Wikipedia page lists 25+ similar apps that are written in all kinds of other languages, so there's likely an option you can use.
Use python build in http server
# sudo python -m http.server 80
Serving HTTP on 0.0.0.0 port 80 ...
123.123.12.1 - - [12/Feb/2018 11:34:02] "GET / HTTP/1.1" 200 -

How to unit test my Google OpenID consumer app?

My web app is a Google OpenID consumer (with Attribute Exchange and OAuth Extension) and I need to write some unit test for it [edit: to test the unit that is responsible to interact with google].
The problem is that default OpenID login procedure needs user interaction (entering user/pass) which is not possible in unit test.
Do you have any idea how can I solve this problem and unit test my OpenID consumer app?
(I prefer not to run my own OpenID provider.)
You need to use a remote controlled browser for this. Selenium was made for this use case.
(indeed they are called functional tests then).
Search on Google for the best way to integrate selenium tests into your web framework.
If I understand you want to test your all application and not just "unit test" it.
The actual test framework depends on the technology your application is using. For example there are many UI and web automation tools that can do what you want.
You should also unit test your core functionality or at least write several integration tests that work against an actual Openid provider but instead of running the entire application just test the functionality of the class (if you're using language that has classes) to make sure it can get the b.
I would also write a couple of unit tests that call a fake provider to test how your code behaves in case of error, connection problems and plain vanilla responses.