Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Commit

Permalink
Add new local testing workflow (close snowplow#686)
Browse files Browse the repository at this point in the history
  • Loading branch information
mhadam committed Jan 18, 2019
1 parent 3b82ea8 commit 6374b7f
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 3 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ node_modules
**/lodash.js
tests/pages/*.js
tests/pages/integration.html
tests/local/serve/
tags/tag.min.js

# creds for Grunt
Expand Down
15 changes: 15 additions & 0 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,11 @@ module.exports = function(grunt) {
files: {
'tests/pages/snowplow.js': 'tests/pages/bundle.js'
}
},
local: {
files: {
'tests/local/serve/snowplow.js': 'tests/pages/bundle.js'
}
}
},

Expand All @@ -133,6 +138,15 @@ module.exports = function(grunt) {
},
src: ['tests/pages/integration-template.html'],
dest: 'tests/pages/integration.html'
},
local: {
options: {
'process': function(src, filepath) {
return src.replace(/'\<\%= subdomain \%\>' \+ '\.ngrok\.io'/g, '\'127.0.0.1:8000\'');
}
},
src: ['tests/pages/integration-template.html'],
dest: 'tests/local/serve/integration.html'
}
},

Expand Down Expand Up @@ -266,4 +280,5 @@ module.exports = function(grunt) {
grunt.registerTask('test', 'Intern tests', ['browserify:test', 'babel:test', 'intern']);
grunt.registerTask('travis', 'Intern tests for Travis CI', ['concat:test', 'browserify:test', 'babel:test', 'intern']);
grunt.registerTask('tags', 'Minifiy the Snowplow invocation tag', ['uglify:tag', 'concat:tag']);
grunt.registerTask('local', 'Builds and places files read to serve and test locally', ['browserify:test', 'concat:local', 'babel:local']);
};
15 changes: 15 additions & 0 deletions local_test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/usr/bin/env bash
cd core
grunt
cd ..
grunt local
if [[ "$(uname)" == "Darwin" ]]; then
(sleep 1 && open http:127.0.0.1:8000/integration.html)&
elif [[ "$(expr substr $(uname -s) 1 5)" == "Linux" ]]; then
(sleep 1 && xdg-open http:127.0.0.1:8000/integration.html)&
elif [[ "$(expr substr $(uname -s) 1 10)" == "MINGW32_NT" ]]; then
:
elif [[ "$(expr substr $(uname -s) 1 10)" == "MINGW64_NT" ]]; then
:
fi
python3 ./tests/local/http-server.py
71 changes: 71 additions & 0 deletions tests/local/http-server.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import os
import sys
from http.server import HTTPServer, BaseHTTPRequestHandler
from http.client import NO_CONTENT
from pprint import pprint


gif_string = b'GIF87a\x01\x00\x01\x00\x80\x00\x00\xff\xff\xff\xff\xff\xff,\x00\x00\x00\x00\x01\x00\x01\x00\x00\x02\x02D\x01\x00;'
file_path = os.path.dirname(__file__)

def get_serve(file):
return os.path.join(file_path, 'serve', file)

class SimpleHTTPRequestHandler(BaseHTTPRequestHandler):

def do_HEAD(self):
self.send_response(200)
if (self.path == '/integration.html'):
self.send_header("Content-type", "text/html")
self.end_headers()
else:
self.send_header("Content-type", "image/gif")
self.end_headers()


def do_GET(self):
"""Respond to a GET request."""
self.send_response(200)
if (self.path == '/integration.html'):
self.send_header("Content-type", "text/html")
self.end_headers()
f = open(get_serve('integration.html'), "rb")
body = f.read()
f.close()
self.wfile.write(body)
elif (self.path == '/snowplow.js'):
self.send_header("Content-type", "text/html")
self.end_headers()
f = open(get_serve('snowplow.js'), "rb")
body = f.read()
f.close()
self.wfile.write(body)
else:
self.send_header("Content-type", "image/gif")
self.end_headers()
self.wfile.write(gif_string)

def do_POST(self):
self.send_response(200)
self.send_header("Content-type", "image/gif")
self.end_headers()
self.wfile.write(''.encode())
#content_len = int(self.headers.get('content-length', 0))
#post_body = self.rfile.read(content_len)
#pprint(post_body)

def do_OPTIONS(self):
self.send_response(200)
self.send_header('Access-Control-Allow-Origin', 'null')
self.send_header('Access-Control-Allow-Methods', 'GET, POST, OPTIONS')
self.send_header("Access-Control-Allow-Headers", "X-Requested-With")
self.send_header("Access-Control-Allow-Headers", "Content-Type")
self.end_headers()


httpd = HTTPServer(('127.0.0.1', 8000), SimpleHTTPRequestHandler)
try:
httpd.serve_forever()
except KeyboardInterrupt:
print('User closed server with Ctrl-c')
sys.exit(0)
6 changes: 3 additions & 3 deletions tests/pages/integration-template.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<!DOCTYPE html>
<html>
<head>
<title>Integration test page</title>
<title>Integration test page</title>
</head>
<body>
<p id="title">Page for sending requests to request_recorder</p>
Expand All @@ -20,9 +20,9 @@

// Taken from a random environment variable

var subdomain = '<%= subdomain %>';
var collector_endpoint = '<%= subdomain %>' + '.ngrok.io';

window.snowplow('newTracker', 'cf', subdomain + '.ngrok.io', {
window.snowplow('newTracker', 'cf', collector_endpoint, {
encodeBase64: true,
appId: 'CFe23a',
platform: 'mob',
Expand Down

0 comments on commit 6374b7f

Please sign in to comment.