File: server_test.go

package info (click to toggle)
golang-github-revel-revel 0.12.0+dfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 828 kB
  • sloc: xml: 7; makefile: 4
file content (103 lines) | stat: -rw-r--r-- 2,659 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package revel

import (
	"net/http"
	"net/http/httptest"
	"os"
	"path"
	"strings"
	"testing"
)

// This tries to benchmark the usual request-serving pipeline to get an overall
// performance metric.
//
// Each iteration runs one mock request to display a hotel's detail page by id.
//
// Contributing parts:
// - Routing
// - Controller lookup / invocation
// - Parameter binding
// - Session, flash, i18n cookies
// - Render() call magic
// - Template rendering
func BenchmarkServeAction(b *testing.B) {
	benchmarkRequest(b, showRequest)
}

func BenchmarkServeJson(b *testing.B) {
	benchmarkRequest(b, jsonRequest)
}

func BenchmarkServePlaintext(b *testing.B) {
	benchmarkRequest(b, plaintextRequest)
}

// This tries to benchmark the static serving overhead when serving an "average
// size" 7k file.
func BenchmarkServeStatic(b *testing.B) {
	benchmarkRequest(b, staticRequest)
}

func benchmarkRequest(b *testing.B, req *http.Request) {
	startFakeBookingApp()
	b.ResetTimer()
	resp := httptest.NewRecorder()
	for i := 0; i < b.N; i++ {
		handle(resp, req)
	}
}

// Test that the booking app can be successfully run for a test.
func TestFakeServer(t *testing.T) {
	startFakeBookingApp()

	resp := httptest.NewRecorder()

	// First, test that the expected responses are actually generated
	handle(resp, showRequest)
	if !strings.Contains(resp.Body.String(), "300 Main St.") {
		t.Errorf("Failed to find hotel address in action response:\n%s", resp.Body)
		t.FailNow()
	}
	resp.Body.Reset()

	handle(resp, staticRequest)
	sessvarsSize := getFileSize(t, path.Join(BasePath, "public", "js", "sessvars.js"))
	if int64(resp.Body.Len()) != sessvarsSize {
		t.Errorf("Expected sessvars.js to have %d bytes, got %d:\n%s", sessvarsSize, resp.Body.Len(), resp.Body)
		t.FailNow()
	}
	resp.Body.Reset()

	handle(resp, jsonRequest)
	if !strings.Contains(resp.Body.String(), `"Address":"300 Main St."`) {
		t.Errorf("Failed to find hotel address in JSON response:\n%s", resp.Body)
		t.FailNow()
	}
	resp.Body.Reset()

	handle(resp, plaintextRequest)
	if resp.Body.String() != "Hello, World!" {
		t.Errorf("Failed to find greeting in plaintext response:\n%s", resp.Body)
		t.FailNow()
	}

	resp.Body = nil
}

func getFileSize(t *testing.T, name string) int64 {
	fi, err := os.Stat(name)
	if err != nil {
		t.Errorf("Unable to stat file:\n%s", name)
		t.FailNow()
	}
	return fi.Size()
}

var (
	showRequest, _      = http.NewRequest("GET", "/hotels/3", nil)
	staticRequest, _    = http.NewRequest("GET", "/public/js/sessvars.js", nil)
	jsonRequest, _      = http.NewRequest("GET", "/hotels/3/booking", nil)
	plaintextRequest, _ = http.NewRequest("GET", "/hotels", nil)
)