Skip to content

Commit

Permalink
Use httptest.Server to provide local test server in TestHTTPContent() (
Browse files Browse the repository at this point in the history
…#185)

Use httptest.Server to provide local test server in TestHTTPContent()

https://pkg.go.dev/net/http/httptest#Server

---------

Co-authored-by: Jonathan C. Dietrich <[email protected]>
Co-authored-by: Jonathan Dietrich <[email protected]>
  • Loading branch information
3 people authored Sep 5, 2024
1 parent b093366 commit e9640eb
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions pkg/vars/stream_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package vars
import (
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -44,7 +46,13 @@ func TestReadFile(t *testing.T) {
func TestHTTPContent(t *testing.T) {
st := Stream{}

k := "http.contents(https://golang.org/)"
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte("Go\n"))
}))
defer server.Close()

k := fmt.Sprintf("http.contents(%s)", server.URL)
holders := []string{k}

result := st.Fill(holders)
Expand All @@ -53,7 +61,7 @@ func TestHTTPContent(t *testing.T) {
t.Errorf("Stream key not found")
}

if !strings.Contains(v[0], "Go") {
if strings.TrimSpace(v[0]) != "Go" {
t.Errorf("Couldn't get the content. Value: %s", v)
}
}
Expand Down

0 comments on commit e9640eb

Please sign in to comment.