
Security News
TypeScript is Porting Its Compiler to Go for 10x Faster Builds
TypeScript is porting its compiler to Go, delivering 10x faster builds, lower memory usage, and improved editor performance for a smoother developer experience.
When you are writing acceptance/integration tests for an application which makes HTTP requests to a remote application, sometimes you need to be able to test the interactions in different scenarios without talking to a real instance of the remote application.
FakeTTP is a standalone web application that allows you to mock requests (ie set and verify expectations on the requests your application makes, and return suitable responses to those requests).
Add GitHub as a gem source (you only need to do this once):
gem sources -a http://gems.github.com
Then install FakeTTP:
sudo gem install kerryb-fakettp
Alternatively, you can specify the source when you install the gem:
sudo gem install kerryb-fakettp --source http://gems.github.com
You can install FakeTTP anywhere that your web server user can see it:
fakettp install <directory>
FakeTTP should work with any Rack-compatible server: just point the server to the correct directory. For example, using Passenger (mod_rails) with Apache, create a virtual host along these lines:
<VirtualHost *:80>
ServerName fakettp.local
DocumentRoot "/path/to/fakettp/public"
<directory "/path/to/fakettp/public">
Order deny,allow
Deny from all
Allow from 127.0.0.1
</directory>
</VirtualHost>
Then make sure fakettp.local
resolves to 127.0.0.1 (assuming you’re running the simulator on the same machine as the application under test), eg by adding the following line to /etc/hosts
:
127.0.0.1 fakettp.local
Because expectations are set by posting Ruby code to be executed on the server, you probably don’t want any old Tom, Dick or Harry to be able to connect. The security settings in the virtual host config example above restrict access to clients running on the local machine.
To reset FakeTTP (ie remove all expectations and errors), make an HTTP POST request to http://fakettp.local/reset
.
To create a new expectation, make an HTTP POST request to http://fakettp.local/expect
, with a Content-Type header of ‘text/plain’ and the request data containing a Ruby block to execute.
The supplied code should be in the following format, and will generally consist of a number of assertions on the request, followed by creation of the response to return to the application under test.
expect "GET of /foo" do request.host.should == 'fakettp.local' request.path_info.should == '/foo'
content_type 'text/plain' "All is well\n"
end
The label on the first line is used in error reporting.
The expectation code has access to the underlying Sinatra request and response objects etc, as well as RSpec matchers.
To verify that all expectations have been met, make an HTTP GET request to http://fakettp.local/verify
.
If all is well, the response will be a 200 OK with a body of ‘OK’. Otherwise the status will be 400 Bad Request, with a list of failures in the body. The failure messages include the complete details of the unexpected request that was received, to assist debugging.
To have FakeTTP respond to multiple hostnames, create the appropriate hosts entries. If you’re using name-based virtual hosts in Apache, add a ServerAlias entry to the virtual host config, under the ServerName line, eg:
ServerAlias foo.com bar.com
0.2.4.1 (5 May 2009)
0.2.4 (25 Mar 2009)
0.2.3 (19 Mar 2009)
0.2.2 (18 Mar 2009)
0.2.1 (17 Mar 2009)
0.2 (14 Mar 2009)
If you get an ‘unexpected return’ error, remove the return statement from your expectation, and just put the return value on the last line of the expect block.
0.1.2 (13 Feb 2009)
0.1.1 (13 Feb 2009)
0.1.0 (13 Feb 2009)
FAQs
Unknown package
We found that kerryb-fakettp demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
TypeScript is porting its compiler to Go, delivering 10x faster builds, lower memory usage, and improved editor performance for a smoother developer experience.
Research
Security News
The Socket Research Team has discovered six new malicious npm packages linked to North Korea’s Lazarus Group, designed to steal credentials and deploy backdoors.
Security News
Socket CEO Feross Aboukhadijeh discusses the open web, open source security, and how Socket tackles software supply chain attacks on The Pair Program podcast.