Skip to content

Commit

Permalink
for mock server
Browse files Browse the repository at this point in the history
  • Loading branch information
hyorigo committed Jul 22, 2024
1 parent 7e5e9ce commit 305fd8b
Showing 1 changed file with 59 additions and 1 deletion.
60 changes: 59 additions & 1 deletion lib/net/ping_test.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,42 @@
package net_test

import (
"net/http"
"net/http/httptest"
"runtime"
"testing"

itn "github.com/1set/starlet/internal"
"github.com/1set/starlet/lib/net"
"go.starlark.net/starlark"
)

// A helper function to create a mock server that returns the specified status code
func createMockServer(statusCode int) *httptest.Server {
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(statusCode)
})
return httptest.NewServer(handler)
}

// Create a mock server that returns a 301 status code with a Location header
func createRedirectMockServer(location string) *httptest.Server {
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Location", location)
w.WriteHeader(http.StatusMovedPermanently)
})
return httptest.NewServer(handler)
}

func TestLoadModule_Ping(t *testing.T) {
// create mock servers for testing
server301 := createRedirectMockServer("https://notgoingthere.invalid")
defer server301.Close()
server404 := createMockServer(http.StatusNotFound)
defer server404.Close()
server500 := createMockServer(http.StatusInternalServerError)
defer server500.Close()

isOnWindows := runtime.GOOS == "windows"
tests := []struct {
name string
Expand Down Expand Up @@ -115,6 +143,31 @@ func TestLoadModule_Ping(t *testing.T) {
assert.true(s.min > 0)
`),
},
{
name: `httping: ignore redirect`,
script: itn.HereDoc(`
load('net', 'httping')
s = httping(server_301, interval=0.1)
assert.eq(s.total, 4)
assert.eq(s.success, 4)
`),
},
{
name: `httping: status 404`,
script: itn.HereDoc(`
load('net', 'httping')
s = httping(server_404, interval=0.1)
`),
wantErr: `net.httping: no successful connections`,
},
{
name: `httping: status 500`,
script: itn.HereDoc(`
load('net', 'httping')
s = httping(server_500, interval=0.1)
`),
wantErr: `net.httping: no successful connections`,
},
{
name: `httping: not exists`,
script: itn.HereDoc(`
Expand Down Expand Up @@ -154,7 +207,12 @@ func TestLoadModule_Ping(t *testing.T) {
t.Skipf("Skip test on Windows")
return
}
res, err := itn.ExecModuleWithErrorTest(t, net.ModuleName, net.LoadModule, tt.script, tt.wantErr, nil)
extra := starlark.StringDict{
"server_301": starlark.String(server301.URL),
"server_404": starlark.String(server404.URL),
"server_500": starlark.String(server500.URL),
}
res, err := itn.ExecModuleWithErrorTest(t, net.ModuleName, net.LoadModule, tt.script, tt.wantErr, extra)
if (err != nil) != (tt.wantErr != "") {
t.Errorf("net(%q) expects error = '%v', actual error = '%v', result = %v", tt.name, tt.wantErr, err, res)
return
Expand Down

0 comments on commit 305fd8b

Please sign in to comment.