aflatter.de

Articles

  • Testing custom headers and ssl with Cucumber and Capybara

    Today I ran into yet another issue where a little hack is required to get the job done. I was testing our application and came to a point where ssl is required. In plain old webrat you could do:

    header "HTTPS", "on"
    

    Even simpler, when using Rack::Test directly, you can just use the third parameter of your favorite request method to pass additional stuff:

    get :index, {}, { :https => 'on' }
    

    If you try to test for custom headers with capybara, you will run into trouble. There is simply no method to get access to the request environment, so things become a little more difficult. The following solution will work only if you use the :rack_test driver. Any other drivers do not support setting headers. Simply put the following code into features/support/headers_hack.rb which will automatically be loaded by cucumber.

    module RackTestMixin
    
      def self.included(mod)
        mod.class_eval do
          # This is where we save additional entries.
          def hacked_env
            @hacked_env ||= {}
          end
          
          # Alias the original method for further use.
          alias_method  :original_env, :env
    
          # Override the method to merge additional headers.
          # Plus this implicitly makes it public.
          def env
            original_env.merge(hacked_env)
          end
        end
      end
    
    end
    
    Capybara::Driver::RackTest.send :include, RackTestMixin
    
    module HeadersHackHelper
      
      def add_headers(headers)
        page.driver.hacked_env.merge!(headers)
      end
    
    end
    
    World(HeadersHackHelper)
    

    It will give you access to an additional attribute of the driver, #hacked_env. Use this hash to set additional entries as you like. Some examples:

    # To test ssl
    Given /^I use ssl$/ do
      add_headers('HTTPS', 'on')
    end
    
    # To set the remote ip
    Given /^my IP is (\d{1,3}\.){3}\d{1,3}$/ do |ip|
      add_headers('REMOTE_ADDR', ip)
    end
    
    # If you check for user agent
    Given /^my user agent is "(.+)"$/ do |agent|
      add_headers('User-Agent', agent)
    end
    

    read more

  • Github Pages and nanoc

    I just found out that my old shabby weblog running Wordpress has been exploited by some spam bots. This is not unusual - no matter if the blog is maintained or not. To deal with this whole mess, I finally abandoned Wordpress in favor of nanoc and Github pages. This is how I set up my repository:

    Github pages will serve your site from the master branch, so we will use the nanoc output for our initial commit. Compile your site with nanoc3 co and move the output directory outside of your site directory. Change to the directory, initialize a new git repository with git init . and add the github remote with git remote add origin git@github.com:username/username.github.com.git. Now stage all the files with git add . and commit them with git commit -m 'Initial commit'. All the steps listed again:

    nanoc3 co
    mv output ..
    cd ../output
    git init .
    git remote add origin git@github.com:username/username.github.com.git
    git add .
    git commit -m 'Initial commit'
    git push origin master
    

    This is not new to you, I know. :)

    But of course you do want to keep your source in the repository too, so here are the steps to achieve that (found on the pages documentation): Create a new root branch (named source in my case) and remove your old index. git clean removes all untracked files - which every file is because you just removed the index.

    git symbolic-ref HEAD refs/heads/source
    rm .git/index
    git clean -fdx
    

    Now you can add your site to the repository and push it to the source branch.

    cp -R /path/to/your/site/ .
    git add .
    git commit -m 'Initial commit of source'
    git push origin source
    

    That’s it.

    To deploy, you can now issue the following commands:

    rm -rf output
    git checkout source
    nanoc3 co
    git checkout master
    cp -R output/* .
    git add .
    git commit -a -m 'I did something cool to my site'
    git push origin master
    

    git commit -a adds all files that are untracked and removes files that are not present in the working tree, so you don’t have to worry about abandoned files.

    To automate this process, I use the following bash script:

    #!/bin/bash
    
    rm -rf output
    git checkout source
    nanoc3 co
    git checkout master
    cp -R output/* .
    git add .
    git commit -a -m "Updated site on `date`"
    git push origin master
    git checkout source
    

    read more

  • Cheating Facebook's Biotronic App

    Okay, so this is crazy. There’s this nice little game called Biotronic on Facebook:

    A puzzle game that features colorful biotechnology. Easy mouse controls, exploding combos and beautiful animations. Challenge yourself and your friends in this exciting game.

    I sometimes played that and achieved a score of about 35k. I already thought about hacking the game via image processing etc. - and being curious, I just used google.

    And guess what? Yes, some guy already did that. It’s cross platform and all kind of stuff and open source:

    http://gitorious.org/web-automation/biocheat

    Interesting project!

    read more