Skip to content
This repository has been archived by the owner on Jan 23, 2018. It is now read-only.

Commit

Permalink
Merge branch 'scheme_rack_apps' of git://github.com/JackDanger/bus-sc…
Browse files Browse the repository at this point in the history
…heme into web
  • Loading branch information
technomancy committed Jun 3, 2008
2 parents d009ab4 + 3fa355f commit cf0e4f4
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 13 deletions.
3 changes: 2 additions & 1 deletion lib/bus_scheme/primitives.rb
Expand Up @@ -26,7 +26,8 @@ def self.special_form(identifier, value)
define 'now', primitive { Time.now }
define 'regex', primitive { |r| Regexp.new(Regexp.escape(r)) }

define 'read', primitive { gets }
define 'read', primitive {|*args| args.empty? ? gets : File.read(args.first) }
# TODO: give read and write the same interface
define 'write', primitive { |obj| puts obj.inspect; 0 }
define 'display', primitive { |obj| puts obj }

Expand Down
1 change: 1 addition & 0 deletions lib/bus_scheme/web.rb
Expand Up @@ -6,6 +6,7 @@
require 'bus_scheme/web/client'
require 'bus_scheme/web/resource'
require 'bus_scheme/web/collection'
require 'bus_scheme/web/rack_app'

module BusScheme
module Web
Expand Down
6 changes: 3 additions & 3 deletions lib/bus_scheme/web/client.rb
Expand Up @@ -2,13 +2,13 @@
require 'uri'

module BusScheme
define('http-get', primitive do |uri, options|
BusScheme['http-method'].call('get', uri, options)
define('web-get', primitive do |uri, options|
BusScheme['http-method'].call(['get', uri, options])[:body.sym]
end)

define('http-post', primitive do |uri, options|
options ||= {}
BusScheme['http-method'].call('post', uri, options)
BusScheme['http-method'].call(['post', uri, options])
end)

# TODO: should we return a hash, or just the body? How do we offer
Expand Down
22 changes: 22 additions & 0 deletions lib/bus_scheme/web/rack_app.rb
@@ -0,0 +1,22 @@
module BusScheme

define 'defwebapp', primitive {|*args| Web::App.new(*args) }

module Web
class App
def initialize(path, contents)
raise "defwebapp needs to be called with a Lambda" unless contents.is_a? Lambda
@contents = contents
Resource[path] = self
BusScheme::Web.serve
end

def call(env)
status, headers, body = *@contents.call(BusScheme.cons(env))
headers = Hash[*headers]
# TODO: sanitize headers and status code
[status, headers, body]
end
end
end
end
13 changes: 13 additions & 0 deletions test/test_primitives.rb
@@ -1,5 +1,6 @@
$LOAD_PATH << File.dirname(__FILE__)
require 'test_helper'
require 'stringio'

class PrimitivesTest < Test::Unit::TestCase
def test_test_framework
Expand Down Expand Up @@ -46,6 +47,18 @@ def test_boolean_short_circuit
assert_evals_to false, "(and #f (assert #f))"
assert_evals_to true, "(or #t (assert #f))"
end

def test_read_with_filename_reads_file_into_a_string
filename = File.join(File.dirname(__FILE__), '..', 'COPYING')
assert_equal File.read(filename), eval!("(read \"#{filename}\")")
end

def test_read_without_filename_gets_from_stdin
$stdin = StringIO.new('gets test')
assert_equal 'gets test', eval!("(read)")
ensure
$stdin = STDIN
end

def test_booleans
assert_evals_to false, '#f'
Expand Down
55 changes: 46 additions & 9 deletions test/test_web.rb
Expand Up @@ -5,8 +5,8 @@
if defined? BusScheme::Web::Resource
module BusScheme
module_function
def Web.web_server # need to expose this for MockRequest
@web_server
def Web.server # need to expose this for MockRequest
@server
end
end

Expand All @@ -31,6 +31,16 @@ def setup
eval! '(defresource "/" concourse-splash)'

eval! '(defresource "/time" (lambda (env) (send (now) (quote to_s))))'

# a very basic rack app in scheme registered at "/simple"
simple_lambda = '(lambda (env) (quote ("200" ("Content-Type" "text/plain") "This is Simple")))'
@simple_app = eval!(simple_lambda)
eval! "(defwebapp \"/simple\" #{simple_lambda})"

# this app returns the SERVER_INFO from the env passed to it
# TODO: come up with a better method for easily returning these values
eval! '(defwebapp "/who-am-i" (lambda (env)
(list "200" (quote ("Content-Type" "text/html")) (env "PATH_INFO"))))'
end

def test_serves_string_resource
Expand Down Expand Up @@ -115,16 +125,43 @@ def test_deletes_resource
assert_response_code 404
end

def test_app_can_be_called
@simple_app.call({'PATH_INFO' => '/simple'})
end

def test_returns_status_code
get '/simple'
assert_response_code 200
end

def test_returns_content_type
get '/simple'
assert_equal 'text/plain', @response.headers['Content-Type']
end

def test_returns_body
get '/simple'
assert_equal 'This is Simple', @response.body
end

def test_returning_environment_as_body
get '/who-am-i'
assert_equal "/who-am-i", @response.body
assert_equal 'text/html', @response.headers['Content-Type']
assert_response_code 200
end

# def test_returns_forbidden_when_unauthorized
# end

# def test_client
# assert BusScheme['http-method'].body
# eval! "(define root-string \"This is the root of our HTTP!\")
# (defresource \"/\" root-string)
def test_client
assert BusScheme['http-method'].body
eval! "(define root-string \"This is the root of our HTTP!\")
(defresource \"/\" root-string)
(web-get \"http://localhost:2000/\")"
end

# (http-get \"http://localhost:2000/\")"
# end
private

def get(path)
Expand All @@ -144,7 +181,7 @@ def delete(path)
end

def mock_request
Rack::MockRequest.new(BusScheme::Web.web_server)
Rack::MockRequest.new(BusScheme::Web.server)
end

def assert_response expected, message = nil
Expand Down

0 comments on commit cf0e4f4

Please sign in to comment.