Skip to content

Instantly share code, notes, and snippets.

@johnboxall
johnboxall / mock.py
Last active December 18, 2015 02:18
Not sure if httpbin is down or your tests are failing? Use `mock` to stub out your external calls!
import mock
import requests
import unittest
def get():
# Retrieves data from an external service eg. Twitter, Google
try:
return requests.get('http://external-service.com/')
except requests.RequestException:
return None
@johnboxall
johnboxall / gist:5695301
Created June 2, 2013 23:14
Go's default redirect behaviour seems to merge slashes in HTTP redirects. How can you override this behaviour?
package main
import (
"testing"
"net/http"
"net/http/httptest"
)
func TestMergeSlashes(t *testing.T) {
req, _ := http.NewRequest("GET", "http://foo.com/", nil)
<!doctype html>
<html>
<head>
<script src="http://httpbin.org/delay/10"></script>
<script>document.write("<plaintext>");</script>
<script src="preparsed.js"></script>
<title>Pre-Parser Test</title>
</head>
<body>
<p>Check the wire level requests generated by this page.<p>
<html>
<head>
<script>
/**
* In 1s, `onError` will be called to 'recover' the original document.
*/
onError = function() {
var s;
while (s = document.scripts[0]) s.parentNode.removeChild(s);
@johnboxall
johnboxall / config.rb
Created March 2, 2013 20:05
Compass `asset_cache_buster` based on file MD5 content hash rather than mtime.
# Generate cache busters using the MD5 digest of files rather than the default
# `mtime`.
asset_cache_buster do |_, file|
Digest::MD5.hexdigest(File.read(file.path))
end
@johnboxall
johnboxall / gunzip.js
Created November 8, 2011 01:30
nodejs: gunzip httpclientresponse w/ zlib
var http = require('http');
var zlib = require('zlib');
var request = http.get({
host: 'www.bonobos.com',
port: 80,
path: '/',
headers: {'accept-encoding': 'gzip'}
});
@johnboxall
johnboxall / jquery.type.js
Created September 4, 2011 02:40
For when you _really_ wanna change the type of a input with jQuery
var rtype = /^(?:button|input)$/i;
jQuery.attrHooks.type.set = function(elem, value) {
// We can't allow the type property to be changed (since it causes problems in IE)
if (rtype.test(elem.nodeName) && elem.parentNode) {
// jQuery.error( "type property can't be changed" );
// JB: Or ... can it!?
var $el = $(elem);
var insertionFn = 'after';
from django.core.exceptions import ObjectDoesNotExist
class InfoMiddleware(object):
'''
Record refer and user-agent info about this dude.
'''
def process_request(self, request):
if request.user.is_anonymous():
if 'referer' not in request.session:
def view(request, template_name):
def obj_iterator():
for obj in Objs.objects.iterator():
obj.do_something()
yield obj
ctx = {
'objs': obj_iterator
}
@johnboxall
johnboxall / fastcgi.py
Created November 7, 2010 22:36
Override BaseFCGIServer.error to use Django error handling.
# Override BaseFCGIServer.error to use Django error handling.
# http://trac.saddi.com/flup/browser/flup/server/fcgi_base.py#L1210
def patch_error(self, req):
import sys
from django.conf import settings
from django.core import urlresolvers
from django.core.handlers.wsgi import WSGIRequest
urlconf = settings.ROOT_URLCONF
urlresolvers.set_urlconf(urlconf)