11package esi_test
22
33import (
4+ "fmt"
45 "net/http"
56 "net/http/httptest"
67 "os"
8+ "strings"
79 "testing"
810
911 "github.com/darkweak/go-esi/esi"
@@ -62,7 +64,13 @@ var expected = map[string]string{
6264func verify (t * testing.T , fixture string ) {
6365 t .Helper ()
6466
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 ] {
6674 t .Errorf ("ESI parsing mismatch from `%s` expected\n Expected:\n %+v\n Given:\n %+v\n " , fixture , expected [fixture ], result )
6775 }
6876}
@@ -114,10 +122,17 @@ func Test_Parse_fullMock(t *testing.T) {
114122
115123// Benchmarks.
116124func 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+
117129 for i := 0 ; i < b .N ; i ++ {
118130 esi .Parse (
119131 []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+ ),
121136 ),
122137 httptest .NewRequest (http .MethodGet , "http://domain.com:9080" , nil ),
123138 )
@@ -132,9 +147,13 @@ var remove = `<esi:include src="http://domain.com:9080/chained-esi-include-1"/>
132147<esi:include src="http://domain.com:9080/chained-esi-include-1"/>`
133148
134149func 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+
135154 for i := 0 ; i < b .N ; i ++ {
136155 esi .Parse (
137- []byte (remove ),
156+ []byte (strings . ReplaceAll ( remove , "domain.com:9080" , server . URL ) ),
138157 httptest .NewRequest (http .MethodGet , "http://domain.com:9080" , nil ),
139158 )
140159 }
@@ -171,9 +190,13 @@ const full = `<html>
171190`
172191
173192func 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+
174197 for i := 0 ; i < b .N ; i ++ {
175198 esi .Parse (
176- []byte (full ),
199+ []byte (strings . ReplaceAll ( full , "domain.com:9080" , server . URL ) ),
177200 httptest .NewRequest (http .MethodGet , "http://domain.com:9080" , nil ),
178201 )
179202 }
0 commit comments