Duplicate a git repository with all branches and tags
In order to clone a git repository including all branches and tags you need to use two parameters when cloning the old and pushing to the new repository respectively:
git clone --bare http://example.com/old-repo.git
cd old-repo
git push --mirror http://example.com/new-repo.git
Of course, the URLs to your repository might look different depending on the protocol used, username required, etc.
For a user git
using the git protocol, it could be git@example.com:repository-namespace/repository.git
How to find out which type of Spec you are
When you need to find out in which kind of spec you are during run-time, it's definitely possible. It's a lot easier in RSpec 2+.
For example, consider this global before
block where you'd want to run some code for specific specs only:
config.before do
# stuff
that_fancy_method
# more stuff
end
RSpec 2+
If you want to run such a block for a specific type of specs, you can use filters:
config.before do
# stuff
# more stuff
end
config.before :type =...
Browsers will not send a referrer when linking from HTTPS to HTTP
- When your site is on HTTPS and you are linking or redirecting to a HTTP site, the browser will not send a referrer.
- This means the target site will see your traffic as "direct traffic", i.e. they cannot distinguish such hits from a user who directly typed in the URL.
Reasons for this behavior
It's probably because of this RFC:
Clients SHOULD NOT include a Referer header field in a (non-secure) HTTP request if the referring page was transferr...
Howto: Create a self-signed certificate
Option 1: Creating a self-signed certificate with the openssl binary
As igalic commented on this gist.
openssl req -new -newkey rsa:2048 -sha256 -days 365 -nodes -x509 -keyout server.key -out server.crt
Explanation
req -new
Create a new request ...
- -newkey
-
... using a new key ...
rsa:2048
... of type RSA, 2048 bits long.
- -sha1
-
Make sure to use SHA1 as this certificate's hashing algorithm,
- -nodes
-
don't encrypt the key and
-x509
...
How to not die with ActionView::MissingTemplate when clients request weird formats
When HTTP clients make an request they can define which response formats they can process. They do it by adding a header to the HTTP request like this:
Accept: application/json
This means the client will only understand JSON responses.
When a Rails action is done, it will try to render a template for a format that the client understand. This means when all you are HTML templates, a request that only accepts application/json
will raise an error:
An ActionView::MissingTemplate occurred in pages#foo:
Missing templa...
Fixing authentication in legacy applications
Authentication is hard: there are many edge cases, and most users (including yourself) usually only go the "happy path" once and never see the edge cases. If you have rolled your own authentication, or been using older authentication solutions, or resorted to HTTP Basic Authentication, this card will tell you what to do to make your application safe.
Any application that stores sensitive data in the browser
That is: cookies, e.g. by offering a login.
- Ask the admins to [turn on SSL](https://makandracards.com/makandra/1416-integrate-s...
Manage your AWS credentials for multiple accounts
Create a directory mkdir ~/.aws
Initialise git repository cd ~/.aws && git init
Create a git branch with a name you want (e.g. development
for the aws development account credentials).
Add AWS credential file .aws_credentials
:
AWSAccessKeyId=ABCDEFG1234
AWSSecretKey=4321GFEDCBA
Also add your EC2 cert and private key file.
You can add other AWS account depending files like .fog
or .guignol.yml
too.
Create symlinks for some config files like .aws_credentials
and .fog
:
ln -s ~/.aws/.aws_credentials ~/.aws_cred...
OpenStack instance not configuring network (DHCP) correctly
We ran into trouble when adding additional compute units to our railscomplete Hosting environment lately.
VM-instances on the new compute units where booting and requesting private IP addresses via DHCP correctly (DHCPDiscover
), but after the answer of the dnsmasq dhcp server (DHCPOffer
) we did not see any further traffic on the host machine. FYI: The instance should request the IP via DHCPRequest
which in turn should be acknowledged by a DHCPAcknowledgment
packet.
We assumed this DHCP UDP traffic did not...
How to access your Rails session ID
This only works when you actually have a session ID (not the case for Rails' CookieStore, for example):
request.session_options[:id]
# => "142b17ab075e71f2a2e2543c6ae34b94"
Note that it's a bad idea to expose your session ID, so be careful what you use this for.
Controller specs do not persist the Rails session across requests of the same spec
In specs, the session never persists but is always a new object for each request. Data put into the session in a previous request is lost. Here is how to circumvent that.
What's going on?
You are making ActionController::TestRequest
s in your specs, and their #initialize
method does this:
self.session = TestSession.new
This means that each time you say something like "get :index
", the session in your controller will just be a new one, and you won't see ...
parallel_tests: Disable parallel run for tagged scenarios
Note: This technique is confusing and slows down your test suite.
Copy the attached code to features/support
. This gets you a new Cucumber tag @no_parallel
which ensures that the tagged scenario does not run in parallel with other scenarios that are tagged with @no_parallel
. Other scenarios not tagged will @no_parallel
can still run in parallel with the tagged test. Please read the previous sentence again.
This can help when multiple test processes that access a single resource that is hard to shar...
ApacheBench may return "Failed requests" for successful requests
When you use ab
to do some performance benchmarking, you might run into output like this:
Complete requests: 200
Failed requests: 5
(Connect: 0, Receive: 0, Length: 5, Exceptions: 0)
Note that in our example these "Failed requests" actually never failed.\
For some requests, the application just returned a response with a different content length than the first response. This is indicated by the "Length: 5
" bit in the example above.
If you see requests that failed with other kinds of errors, they probably fail...
Rails' Insecure Defaults - Code Climate Blog
Rails’ reputation as a relatively secure Web framework is well deserved. Out-of-the-box, there is protection against many common attacks: cross site scripting (XSS), cross site request forgery (CSRF) and SQL injection. Core members are knowledgeable and genuinely concerned with security.
However, there are places where the default behavior could be more secure. This post explores potential security issues in Rails 3 that are fixed in Rails 4, as well as some that are still risky. I hope this post will help you secure your own apps, as w...
How to set the user agent in tests
The User-Agent
HTTP header identifies the client and is sent by "regular" browsers, search engine crawlers, or other web client software.
Cucumber
In Rack::Test
, you can set your user agent like this on Capybara:
Given /^my user agent is "(.+)"$/ do |agent|
page.driver.browser.header('User-Agent', agent)
# Or, for older Capybaras:
# page.driver.header('User-Agent', agent)
end
For Selenium tests with Firefox, it seems you can set the general.useragent.override
profile setting to your preferred value. [See StackOver...
Test redirects to an external URL with Cucumber/Capybara
When a controller action redirects to an external URL (like http://somehost.com/some/path
) you will find that this is hard to test with Cucumber and Capybara:
- A non-Javascript Rack::Test scenario will just ignore the host and try to open
/some/path
in your local application - A Selenium test will actually follow the redirect, which you probably don't want either
There are two workarounds for this. You can use either, or a combination of both.
- Write a controller spec
Controller specs can test if a resp...
How to disable cookies in cucumber tests
Unfortunately, Capybara does not offer a switch to disable cookies in your test browser. However, you can work around that by using a tiny Rack middleware -- it works for both Selenium and non-Selenium tests.
Wouldn't it be nice to say something like this?
Given cookies are disabled
When I try to sign in
Then I should see "Can't sign you in. Please enable cookies."
You can! Put the code below into some place like lib/rack/cookie_stripper.rb
.
module Rack
class CookieStripper
ENABLED = false
...
Cucumber step to set cookies in your Capybara session
To set a cookie in your test browser for cucumber tests, you need to know which driver you are using. Use the step below according to your driver.
Rack::Test
Given /^I have a "([^\"]+)" cookie set to "([^\"]+)"$/ do |key, value|
headers = {}
Rack::Utils.set_cookie_header!(headers, key, value)
cookie_string = headers['Set-Cookie']
Capybara.current_session.driver.browser.set_cookie(cookie_string)
end
Note that Rack::Utils
is only used to find out the correct cookie header string (you don't want to generate it yours...
Comparing Rails' flash hashes will not respect their internal lists of used entries
Rails flashes (FlashHash
) track a list of used keys, which is not respected when comparing flash hashes.
This does not concern you under most circumstances.
Basics
When ActionController
picks up a flash object, it will call the #sweep
method once; that method checks the list of used flash entries and deletes those. All other entries are flagged as used. This means they will be deleted on the next request, but are still be available for rendering during the current request.
Fun facts: When redirecting, this does not happen. Also,...
Tips And Tricks For Print Style Sheets
Smashing Magazine lists some handy tricks for print style sheets, all with CSS:
- Expand External Links For Print
- Print QR Codes For Easy URL References
- Use CSS filters to improve the result of printed graphics (i.e. images with transparent parts)
- Use your DevTools to preview the print styles in your browser
Capistrano: Bundler stalls and asks for "Username"
Given you use Capistrano together with bundler to automatically install your gems when deploying.
I recently had the problem that Capistrano stalled like this:
[err :: host.name.tld] Username:
It turned out that I this originated from GitHub. We had a gem in our Gemfile that explicitly pointed to a GitHub URL like that:
gem 'foogem', :git => 'https://github.com/blubb/foogem.git'
The URL was returning a 404 which caused the problems. You have to get another gem or point to a fork on GitHub.
How to fix: Session hash does not get updated when using "merge!"
tl;dr: Do not use merge!
for session hashes. Use update
instead.
Outline
Let's assume you're modifying the Rails session. For simplicity, let's also assume your session is empty when you start (same effect when there is data):
# In our example, we're in a Rack middleware
request = Rack::Request.new(env)
request.session.merge! :hello => 'Universe'
request.session
=> {}
Even worse: When you inspect your request.session
like above (e.g. in a debugger shell, o...
How to fix: "Error Bundler::HTTPError during request to dependency API"
If bundle install
shows the following message for you ...
Error Bundler::HTTPError during request to dependency API
... upgrade to Bundler ≥ 1.2.4:
gem install bundler
Apparently, it just hides the message.
Prevent double clicks on link_to_remote (simple case)
This works well in the simplified case, when your link disappears after it was clicked.
Let link_to_remote behave as „disabled“ after the first click. Use the :before
hook to replace the orignal link with a link that does nothing but looks like the original link:
:ruby
label = "do_something"
dummy_link = link_to(label)
other_attributes_hash = { :url => ..., :method => ..., ... }
disable_link_option = { :before => "$('your_link_selector').html('#{escape_javascript(dummy_link)}'" } # jquery
= link_to_remote(label, other_att...
CSS: Matching against attributes and their values (or parts of them)
You probably know that you can use CSS selectors to match against elements and their attributes, such as:
a[title] { /* any <a> that has a "title" */ }
a[data-fancy="true"] { /* any <a> that has their "data-fancy" attribute set to "true" */ }
But there is more: You do not need to match against "full" attribute values but can match against parts of them.
They work in all somewhat modern browsers, and IE9 or later.
Match variants
Exact match (CSS2)
[foo="bar"]
(matches `<div foo="b...