File: results_test.go

package info (click to toggle)
golang-github-revel-revel 1.0.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,240 kB
  • sloc: xml: 7; makefile: 7; javascript: 1
file content (73 lines) | stat: -rw-r--r-- 2,061 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// Copyright (c) 2012-2016 The Revel Framework Authors, All rights reserved.
// Revel Framework source code and usage is governed by a MIT style
// license that can be found in the LICENSE file.

package revel

import (
	"fmt"
	"net/http/httptest"
	"strings"
	"testing"
)

// Added test case for redirection testing for strings
func TestRedirect(t *testing.T) {
	startFakeBookingApp()
	redirect := RedirectToURLResult{fmt.Sprintf("/hotels/index/foo")}
	resp := httptest.NewRecorder()
	c := NewTestController(resp, showRequest)
	redirect.Apply(c.Request, c.Response)
	if resp.Header().Get("Location") != "/hotels/index/foo" {
		t.Errorf("Failed to set redirect header correctly. : %s", resp.Header().Get("Location"))
	}
}

// Test that the render response is as expected.
func TestBenchmarkRender(t *testing.T) {
	startFakeBookingApp()
	resp := httptest.NewRecorder()
	c := NewTestController(resp, showRequest)
	if err := c.SetAction("Hotels", "Show"); err != nil {
		t.Errorf("SetAction failed: %s", err)
	}
	result := Hotels{c}.Show(3)
	result.Apply(c.Request, c.Response)
	if !strings.Contains(resp.Body.String(), "300 Main St.") {
		t.Errorf("Failed to find hotel address in action response:\n%s", resp.Body)
	}
}

func BenchmarkRenderChunked(b *testing.B) {
	startFakeBookingApp()
	resp := httptest.NewRecorder()
	resp.Body = nil
	c := NewTestController(resp, showRequest)
	if err := c.SetAction("Hotels", "Show"); err != nil {
		b.Errorf("SetAction failed: %s", err)
	}
	Config.SetOption("results.chunked", "true")
	b.ResetTimer()

	hotels := Hotels{c}
	for i := 0; i < b.N; i++ {
		hotels.Show(3).Apply(c.Request, c.Response)
	}
}

func BenchmarkRenderNotChunked(b *testing.B) {
	startFakeBookingApp()
	resp := httptest.NewRecorder()
	resp.Body = nil
	c := NewTestController(resp, showRequest)
	if err := c.SetAction("Hotels", "Show"); err != nil {
		b.Errorf("SetAction failed: %s", err)
	}
	Config.SetOption("results.chunked", "false")
	b.ResetTimer()

	hotels := Hotels{c}
	for i := 0; i < b.N; i++ {
		hotels.Show(3).Apply(c.Request, c.Response)
	}
}