Add API Framework Revel Source Files
[iec.git] / src / foundation / api / revel / server_test.go
1 // Copyright (c) 2012-2016 The Revel Framework Authors, All rights reserved.
2 // Revel Framework source code and usage is governed by a MIT style
3 // license that can be found in the LICENSE file.
4
5 package revel
6
7 import (
8         "github.com/stretchr/testify/assert"
9         "net/http"
10         "net/http/httptest"
11         "os"
12         "path/filepath"
13         "strings"
14         "testing"
15         "time"
16 )
17
18 // This tries to benchmark the usual request-serving pipeline to get an overall
19 // performance metric.
20 //
21 // Each iteration runs one mock request to display a hotel's detail page by id.
22 //
23 // Contributing parts:
24 // - Routing
25 // - Controller lookup / invocation
26 // - Parameter binding
27 // - Session, flash, i18n cookies
28 // - Render() call magic
29 // - Template rendering
30 func BenchmarkServeAction(b *testing.B) {
31         benchmarkRequest(b, showRequest)
32 }
33
34 func BenchmarkServeJson(b *testing.B) {
35         benchmarkRequest(b, jsonRequest)
36 }
37
38 func BenchmarkServePlaintext(b *testing.B) {
39         benchmarkRequest(b, plaintextRequest)
40 }
41
42 // This tries to benchmark the static serving overhead when serving an "average
43 // size" 7k file.
44 func BenchmarkServeStatic(b *testing.B) {
45         benchmarkRequest(b, staticRequest)
46 }
47
48 func benchmarkRequest(b *testing.B, req *http.Request) {
49         startFakeBookingApp()
50         b.ResetTimer()
51         resp := httptest.NewRecorder()
52         for i := 0; i < b.N; i++ {
53                 CurrentEngine.(*GoHttpServer).Handle(resp, req)
54         }
55 }
56
57 // Test that the booking app can be successfully run for a test.
58 func TestFakeServer(t *testing.T) {
59         startFakeBookingApp()
60
61         resp := httptest.NewRecorder()
62
63         // First, test that the expected responses are actually generated
64         CurrentEngine.(*GoHttpServer).Handle(resp, showRequest)
65         if !strings.Contains(resp.Body.String(), "300 Main St.") {
66                 t.Errorf("Failed to find hotel address in action response:\n%s", resp.Body)
67                 t.FailNow()
68         }
69         resp.Body.Reset()
70
71         CurrentEngine.(*GoHttpServer).Handle(resp, staticRequest)
72         sessvarsSize := getFileSize(t, filepath.Join(BasePath, "public", "js", "sessvars.js"))
73         if int64(resp.Body.Len()) != sessvarsSize {
74                 t.Errorf("Expected sessvars.js to have %d bytes, got %d:\n%s", sessvarsSize, resp.Body.Len(), resp.Body)
75                 t.FailNow()
76         }
77         resp.Body.Reset()
78
79         CurrentEngine.(*GoHttpServer).Handle(resp, jsonRequest)
80         if !strings.Contains(resp.Body.String(), `"Address":"300 Main St."`) {
81                 t.Errorf("Failed to find hotel address in JSON response:\n%s", resp.Body)
82                 t.FailNow()
83         }
84         resp.Body.Reset()
85
86         CurrentEngine.(*GoHttpServer).Handle(resp, plaintextRequest)
87         if resp.Body.String() != "Hello, World!" {
88                 t.Errorf("Failed to find greeting in plaintext response:\n%s", resp.Body)
89                 t.FailNow()
90         }
91
92         resp.Body = nil
93 }
94
95 func getFileSize(t *testing.T, name string) int64 {
96         fi, err := os.Stat(name)
97         if err != nil {
98                 t.Errorf("Unable to stat file:\n%s", name)
99                 t.FailNow()
100         }
101         return fi.Size()
102 }
103
104 // Ensure on app start runs in order
105 func TestOnAppStart(t *testing.T) {
106         str := ""
107         a := assert.New(t)
108         OnAppStart(func() {
109                 str += " World"
110         }, 2)
111
112         OnAppStart(func() {
113                 str += "Hello"
114         }, 1)
115
116         startFakeBookingApp()
117
118         a.Equal("Hello World", str, "Failed to order OnAppStart")
119 }
120
121 // Ensure on app stop runs in order
122 func TestOnAppStop(t *testing.T) {
123         a := assert.New(t)
124         startFakeBookingApp()
125         i := ""
126         OnAppStop(func() {
127                 i += "cruel world"
128                 t.Logf("i: %v \n", i)
129         }, 2)
130         OnAppStop(func() {
131                 i += "goodbye "
132                 t.Logf("i: %v \n", i)
133         }, 1)
134         go func() {
135                 time.Sleep(2 * time.Second)
136                 RaiseEvent(ENGINE_SHUTDOWN_REQUEST, nil)
137         }()
138         Run(0)
139         a.Equal("goodbye cruel world", i, "Did not get shutdown events")
140
141 }
142
143 var (
144         showRequest, _      = http.NewRequest("GET", "/hotels/3", nil)
145         staticRequest, _    = http.NewRequest("GET", "/public/js/sessvars.js", nil)
146         jsonRequest, _      = http.NewRequest("GET", "/hotels/3/booking", nil)
147         plaintextRequest, _ = http.NewRequest("GET", "/hotels", nil)
148 )