问题
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!
回答1:
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.
回答2:
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
回答3:
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.
回答4:
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 -
来源:https://stackoverflow.com/questions/7966946/fake-http-server-for-testing-purpose