tynn 1.0.0.rc1 → 1.0.0.rc2

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,36 +0,0 @@
1
- require "json"
2
-
3
- module Tynn::JSONParser
4
- def self.setup(app) # :nodoc:
5
- app.use(Tynn::JSONParser::Middleware)
6
- end
7
-
8
- class Middleware # :nodoc:
9
- CONTENT_TYPE = "application/json".freeze
10
- FORM_HASH = "rack.request.form_hash".freeze
11
- FORM_INPUT = "rack.request.form_input".freeze
12
-
13
- def initialize(app)
14
- @app = app
15
- end
16
-
17
- def call(env)
18
- request = Rack::Request.new(env)
19
-
20
- if json?(request) && !(body = request.body.read).empty?
21
- request.body.rewind
22
-
23
- request.env[FORM_HASH] = JSON.parse(body)
24
- request.env[FORM_INPUT] = request.body
25
- end
26
-
27
- return @app.call(request.env)
28
- end
29
-
30
- private
31
-
32
- def json?(request)
33
- return request.media_type == CONTENT_TYPE
34
- end
35
- end
36
- end
@@ -1,78 +0,0 @@
1
- require_relative "../lib/tynn/hmote"
2
-
3
- setup do
4
- Tynn.helpers(Tynn::HMote, views: File.expand_path("./test/views"))
5
-
6
- Tynn::Test.new
7
- end
8
-
9
- test "partial" do |app|
10
- Tynn.define do
11
- on "partial" do
12
- res.write(partial("partial", name: "mote"))
13
- end
14
- end
15
-
16
- app.get("/partial")
17
-
18
- assert_equal "mote", app.res.body.strip
19
- end
20
-
21
- test "view" do |app|
22
- Tynn.define do
23
- on "view" do
24
- res.write(view("view", title: "tynn", name: "mote"))
25
- end
26
- end
27
-
28
- app.get("/view")
29
-
30
- assert_equal "tynn / mote", app.res.body.strip
31
- end
32
-
33
- test "render" do |app|
34
- Tynn.define do
35
- on "render" do
36
- render("view", title: "tynn", name: "mote")
37
- end
38
- end
39
-
40
- app.get("/render")
41
-
42
- assert_equal 200, app.res.status
43
- assert_equal "text/html", app.res.headers["Content-Type"]
44
- assert_equal "tynn / mote", app.res.body.strip
45
- end
46
-
47
- test "404" do |app|
48
- Tynn.define do
49
- on "404" do
50
- res.status = 404
51
-
52
- render("view", title: "tynn", name: "mote")
53
- end
54
- end
55
-
56
- app.get("/404")
57
-
58
- assert_equal 404, app.res.status
59
- assert_equal "text/html", app.res.headers["Content-Type"]
60
- assert_equal "tynn / mote", app.res.body.strip
61
- end
62
-
63
- test "custom layout" do
64
- class App < Tynn
65
- layout("custom_layout")
66
- end
67
-
68
- App.define do
69
- root do
70
- render("view", title: "tynn", name: "mote")
71
- end
72
- end
73
-
74
- app = Tynn::Test.new(App)
75
- app.get("/")
76
-
77
- assert_equal "custom / tynn / mote", app.res.body.strip
78
- end
@@ -1,22 +0,0 @@
1
- require_relative "../lib/tynn/json_parser"
2
-
3
- headers = {
4
- "CONTENT_TYPE" => "application/json"
5
- }
6
-
7
- test "json body parse" do
8
- Tynn.helpers(Tynn::JSONParser)
9
-
10
- params = { "foo" => "foo" }
11
-
12
- Tynn.define do
13
- root do
14
- res.write(JSON.generate(req.params))
15
- end
16
- end
17
-
18
- app = Tynn::Test.new(Tynn)
19
- app.post("/", JSON.generate(params), headers)
20
-
21
- assert_equal params, JSON.parse(app.res.body)
22
- end