1
1
package esi_test
2
2
3
3
import (
4
+ "fmt"
4
5
"net/http"
5
6
"net/http/httptest"
6
7
"os"
8
+ "strings"
7
9
"testing"
8
10
9
11
"github.com/darkweak/go-esi/esi"
@@ -62,7 +64,13 @@ var expected = map[string]string{
62
64
func verify (t * testing.T , fixture string ) {
63
65
t .Helper ()
64
66
65
- if result := string (esi .Parse (loadFromFixtures (fixture ), getRequest ())); result != expected [fixture ] {
67
+ html := loadFromFixtures (fixture )
68
+ server := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
69
+ _ , _ = w .Write ([]byte ("<h1>CHAINED 2</h1>" ))
70
+ }))
71
+ html = []byte (strings .ReplaceAll (string (html ), "http://domain.com:9080" , server .URL ))
72
+
73
+ if result := string (esi .Parse (html , getRequest ())); result != expected [fixture ] {
66
74
t .Errorf ("ESI parsing mismatch from `%s` expected\n Expected:\n %+v\n Given:\n %+v\n " , fixture , expected [fixture ], result )
67
75
}
68
76
}
@@ -114,10 +122,17 @@ func Test_Parse_fullMock(t *testing.T) {
114
122
115
123
// Benchmarks.
116
124
func BenchmarkInclude (b * testing.B ) {
125
+ server := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
126
+ _ , _ = w .Write ([]byte ("<h1>CHAINED 2</h1>" ))
127
+ }))
128
+
117
129
for i := 0 ; i < b .N ; i ++ {
118
130
esi .Parse (
119
131
[]byte (
120
- `<esi:include src="http://domain.com:9080/chained-esi-include-1" alt=http://domain.com:9080/alt-esi-include/>` ,
132
+ fmt .Sprintf (
133
+ `<esi:include src="http://%s/chained-esi-include-1" alt=http://domain.com:9080/alt-esi-include/>` ,
134
+ server .URL ,
135
+ ),
121
136
),
122
137
httptest .NewRequest (http .MethodGet , "http://domain.com:9080" , nil ),
123
138
)
@@ -132,9 +147,13 @@ var remove = `<esi:include src="http://domain.com:9080/chained-esi-include-1"/>
132
147
<esi:include src="http://domain.com:9080/chained-esi-include-1"/>`
133
148
134
149
func BenchmarkRemove (b * testing.B ) {
150
+ server := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
151
+ _ , _ = w .Write ([]byte ("<h1>CHAINED 2</h1>" ))
152
+ }))
153
+
135
154
for i := 0 ; i < b .N ; i ++ {
136
155
esi .Parse (
137
- []byte (remove ),
156
+ []byte (strings . ReplaceAll ( remove , "domain.com:9080" , server . URL ) ),
138
157
httptest .NewRequest (http .MethodGet , "http://domain.com:9080" , nil ),
139
158
)
140
159
}
@@ -171,9 +190,13 @@ const full = `<html>
171
190
`
172
191
173
192
func BenchmarkFull (b * testing.B ) {
193
+ server := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
194
+ _ , _ = w .Write ([]byte ("<h1>CHAINED 2</h1>" ))
195
+ }))
196
+
174
197
for i := 0 ; i < b .N ; i ++ {
175
198
esi .Parse (
176
- []byte (full ),
199
+ []byte (strings . ReplaceAll ( full , "domain.com:9080" , server . URL ) ),
177
200
httptest .NewRequest (http .MethodGet , "http://domain.com:9080" , nil ),
178
201
)
179
202
}
0 commit comments