Skip to content

Commit

Permalink
- wrote decoder
Browse files Browse the repository at this point in the history
- stop using literal test output
- test success = compared to decoded output, not literal comparison of encoded output
  • Loading branch information
Randy Reddig committed Dec 23, 2008
1 parent 63881f5 commit f66df7c
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 20 deletions.
5 changes: 5 additions & 0 deletions .gitignore
@@ -0,0 +1,5 @@
.DS_Store
dns-shutdown.tap
*.pyc
*.pid
*.log
5 changes: 2 additions & 3 deletions csshttprequest.php
Expand Up @@ -14,9 +14,8 @@
function encode($string) {
$quoted = rawurlencode($string);
$out = "";
$n = 0;
for ($i = 0; $i < strlen($quoted); $i+=LENGTH) {
$out .= "#c" . $n . "{background:url(" . PREFIX . substr($quoted, $i, $i+LENGTH) . ");}\n";
for ($i = 0, $n = 0; $i < strlen($quoted); $i += LENGTH, $n++) {
$out .= "#c" . $n . "{background:url(" . PREFIX . substr($quoted, $i, LENGTH) . ");}\n";
}
return $out;
}
Expand Down
15 changes: 15 additions & 0 deletions csshttprequest.py
Expand Up @@ -3,11 +3,13 @@
__author__ = "Randy Reddig - ydnar@nb.io"


import re
import urllib


PREFIX = "data:,"
LENGTH = 2000 - len(PREFIX) # Internet Explorer 2KB URI limit
MATCH_CHR = re.compile(r'\#c(?P<index>\d+)\s*\{\s*background(?:-image)?\:\s*url\(data\:,(?P<value>[^\)]+)\);?\s*}')


def encode(string):
Expand All @@ -20,6 +22,19 @@ def encode(string):
return out


def decode(string):
out = []
rules = string.split('\n')
for rule in rules:
match = re.search(MATCH_CHR, rule)
if match:
index = int(match.group('index'))
value = match.group('value')
out.append('')
out[index] = value
return urllib.unquote(''.join(out))


if __name__ == '__main__':
import sys
sys.stdout.write(encode(sys.stdin.read()))
21 changes: 10 additions & 11 deletions test-all
Expand Up @@ -3,34 +3,33 @@

from __future__ import with_statement
import os
import re
import sys
import glob
import unittest
from subprocess import Popen, PIPE
from csshttprequest import encode, decode


class TestSequenceFunctions(unittest.TestCase):
def setUp(self):
self.input = {}
self.output = {}
self.fixtures = {}
for filename in glob.glob(os.path.join('tests','*.in')):
with open(filename) as f:
self.input[filename.rsplit('.')[0]] = f.read()
for filename in glob.glob(os.path.join('tests','*.out')):
with open(filename) as f:
self.output[filename.rsplit('.')[0]] = self._normalize(f.read())
self.fixtures[filename.rsplit('.')[0]] = f.read()

def _normalize(self, value):
return value.rstrip('\n')

def _test_cmd(self, cmd):
for test in self.input.keys():
sys.stdout.write("\nTesting %s with %s" % (cmd[1], test))
for key in self.fixtures.keys():
fixture = self.fixtures[key]
sys.stdout.write("\nTesting %s with %s" % (cmd[1], key))
pipe = Popen(cmd, stdin=PIPE, stdout=PIPE, close_fds=True)
pipe.stdin.write(self.input[test])
pipe.stdin.write(fixture)
pipe.stdin.close()
output = self._normalize(pipe.stdout.read())
self.assertEqual(output, self.output[test])
output = decode(self._normalize(pipe.stdout.read()))
self.assertEqual(output, fixture)

def test_python(self):
self._test_cmd(['python', 'csshttprequest.py'])
Expand Down
1 change: 0 additions & 1 deletion tests/hello-world.out

This file was deleted.

3 changes: 0 additions & 3 deletions tests/json-flickr.out

This file was deleted.

1 change: 0 additions & 1 deletion tests/json-time.out

This file was deleted.

1 change: 0 additions & 1 deletion tests/lorem.out

This file was deleted.

0 comments on commit f66df7c

Please sign in to comment.