Path: blob/dev/pkg/protocols/offlinehttp/read_response_test.go
2070 views
package offlinehttp12import (3"fmt"4"io"5"net/http"6"net/http/httptest"7"net/http/httputil"8"testing"9"time"1011"github.com/julienschmidt/httprouter"12"github.com/stretchr/testify/require"13)1415func TestReadResponseFromString(t *testing.T) {16expectedBody := `<!DOCTYPE html>17<html>18<head>19<title>Firing Range</title>20</head>21<body>22<h1>Version 0.48</h1>23<h1>What is the Firing Range?</h1>24<p>25</body>26</html>`2728tests := []struct {29name string30data string31}{32{33name: "response",34data: `HTTP/1.1 200 OK35Age: 036Cache-Control: public, max-age=60037Content-Type: text/html38Server: Google Frontend3940<!DOCTYPE html>41<html>42<head>43<title>Firing Range</title>44</head>45<body>46<h1>Version 0.48</h1>47<h1>What is the Firing Range?</h1>48<p>49</body>50</html>`,51},52{53name: "response-http2-without-minor-version",54data: `HTTP/2 200 OK55Age: 056Cache-Control: public, max-age=60057Content-Type: text/html58Server: Google Frontend5960<!DOCTYPE html>61<html>62<head>63<title>Firing Range</title>64</head>65<body>66<h1>Version 0.48</h1>67<h1>What is the Firing Range?</h1>68<p>69</body>70</html>`,71},72{73name: "response-http2-with-minor-version",74data: `HTTP/2.0 200 OK75Age: 076Cache-Control: public, max-age=60077Content-Type: text/html78Server: Google Frontend7980<!DOCTYPE html>81<html>82<head>83<title>Firing Range</title>84</head>85<body>86<h1>Version 0.48</h1>87<h1>What is the Firing Range?</h1>88<p>89</body>90</html>`,91},92{93name: "request-response",94data: `GET http://public-firing-range.appspot.com/ HTTP/1.195Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.996Accept-Encoding: gzip, deflate97Upgrade-Insecure-Requests: 198User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 11_1_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.96 Safari/537.3699100HTTP/1.1 200 OK101Age: 0102Cache-Control: public, max-age=600103Content-Type: text/html104Server: Google Frontend105106<!DOCTYPE html>107<html>108<head>109<title>Firing Range</title>110</head>111<body>112<h1>Version 0.48</h1>113<h1>What is the Firing Range?</h1>114<p>115</body>116</html>`,117},118{119name: "request-response-without-minor-version",120data: `GET http://public-firing-range.appspot.com/ HTTP/1.1121Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9122Accept-Encoding: gzip, deflate123Upgrade-Insecure-Requests: 1124User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 11_1_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.96 Safari/537.36125126HTTP/2 200 OK127Age: 0128Cache-Control: public, max-age=600129Content-Type: text/html130Server: Google Frontend131132<!DOCTYPE html>133<html>134<head>135<title>Firing Range</title>136</head>137<body>138<h1>Version 0.48</h1>139<h1>What is the Firing Range?</h1>140<p>141</body>142</html>`,143},144}145146for _, tt := range tests {147t.Run(tt.name, func(t *testing.T) {148resp, err := readResponseFromString(tt.data)149require.Nil(t, err, "could not read response from string")150151respData, err := io.ReadAll(resp.Body)152require.Nil(t, err, "could not read response body")153require.Equal(t, expectedBody, string(respData), "could not get correct parsed body")154require.Equal(t, "Google Frontend", resp.Header.Get("Server"), "could not get correct headers")155})156}157158t.Run("test-live-response-with-content-length", func(t *testing.T) {159var ts *httptest.Server160router := httprouter.New()161router.GET("/", func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {162w.Header().Add("Server", "Google Frontend")163_, _ = fmt.Fprintf(w, "%s", `<!DOCTYPE html>164<html>165<head>166<title>Firing Range</title>167</head>168<body>169<h1>Version 0.48</h1>170<h1>What is the Firing Range?</h1>171<p>172</body>173</html>`)174})175ts = httptest.NewServer(router)176defer ts.Close()177178client := &http.Client{179Timeout: 3 * time.Second,180}181182data, err := client.Get(ts.URL)183require.Nil(t, err, "could not dial url")184defer func() {185_ = data.Body.Close()186}()187188b, err := httputil.DumpResponse(data, true)189require.Nil(t, err, "could not dump response")190191respData, err := readResponseFromString(string(b))192require.Nil(t, err, "could not read response from string")193194_, err = io.ReadAll(respData.Body)195require.Nil(t, err, "could not read response body")196197require.Equal(t, "Google Frontend", respData.Header.Get("Server"), "could not get correct headers")198199})200}201202203