Add API Framework Revel Source Files
[iec.git] / src / foundation / api / revel / results_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         "fmt"
9         "net/http/httptest"
10         "strings"
11         "testing"
12 )
13
14 // Added test case for redirection testing for strings
15 func TestRedirect(t *testing.T) {
16         startFakeBookingApp()
17         redirect := RedirectToURLResult{fmt.Sprintf("/hotels/index/foo")}
18         resp := httptest.NewRecorder()
19         c := NewTestController(resp, showRequest)
20         redirect.Apply(c.Request, c.Response)
21         if resp.Header().Get("Location") != "/hotels/index/foo" {
22                 t.Errorf("Failed to set redirect header correctly. : %s", resp.Header().Get("Location"))
23         }
24 }
25
26 // Test that the render response is as expected.
27 func TestBenchmarkRender(t *testing.T) {
28         startFakeBookingApp()
29         resp := httptest.NewRecorder()
30         c := NewTestController(resp, showRequest)
31         if err := c.SetAction("Hotels", "Show"); err != nil {
32                 t.Errorf("SetAction failed: %s", err)
33         }
34         result := Hotels{c}.Show(3)
35         result.Apply(c.Request, c.Response)
36         if !strings.Contains(resp.Body.String(), "300 Main St.") {
37                 t.Errorf("Failed to find hotel address in action response:\n%s", resp.Body)
38         }
39 }
40
41 func BenchmarkRenderChunked(b *testing.B) {
42         startFakeBookingApp()
43         resp := httptest.NewRecorder()
44         resp.Body = nil
45         c := NewTestController(resp, showRequest)
46         if err := c.SetAction("Hotels", "Show"); err != nil {
47                 b.Errorf("SetAction failed: %s", err)
48         }
49         Config.SetOption("results.chunked", "true")
50         b.ResetTimer()
51
52         hotels := Hotels{c}
53         for i := 0; i < b.N; i++ {
54                 hotels.Show(3).Apply(c.Request, c.Response)
55         }
56 }
57
58 func BenchmarkRenderNotChunked(b *testing.B) {
59         startFakeBookingApp()
60         resp := httptest.NewRecorder()
61         resp.Body = nil
62         c := NewTestController(resp, showRequest)
63         if err := c.SetAction("Hotels", "Show"); err != nil {
64                 b.Errorf("SetAction failed: %s", err)
65         }
66         Config.SetOption("results.chunked", "false")
67         b.ResetTimer()
68
69         hotels := Hotels{c}
70         for i := 0; i < b.N; i++ {
71                 hotels.Show(3).Apply(c.Request, c.Response)
72         }
73 }