1
0
Fork 0
mirror of https://code.forgejo.org/actions/ovh-dns-update synced 2025-03-15 06:46:59 +01:00
ovh-dns-update/main_test.go
2023-08-18 16:15:07 +02:00

57 lines
1.4 KiB
Go

// SPDX-FileCopyrightText: 2023 Olivier Charvin
// SPDX-License-Identifier: CC0-1.0
package main
import (
"fmt"
"io"
"net/http"
"net/http/httptest"
"os"
"testing"
"github.com/sethvargo/go-githubactions"
)
func TestRun(t *testing.T) {
s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch r.URL.Path {
default:
msg := fmt.Sprintf("unexpected request on %s", r.URL.Path)
http.Error(w, msg, http.StatusBadRequest)
t.Error(msg)
case "/auth/time":
case "/domain/zone/FAKEVALUE-INPUT_DOMAIN/record/FAKEVALUE-INPUT_RECORD-ID":
buf, err := io.ReadAll(r.Body)
if err != nil {
msg := fmt.Sprintf("could not read request body: %v", err)
http.Error(w, msg, http.StatusBadRequest)
t.Error(msg)
}
if string(buf) != `{"subDomain":"FAKEVALUE-INPUT_SUBDOMAIN","target":"\"FAKEVALUE-INPUT_VALUE\""}` {
msg := fmt.Sprintf("unexpected body: %s", string(buf))
http.Error(w, msg, http.StatusBadRequest)
t.Error(msg)
}
}
}))
t.Cleanup(s.Close)
action := githubactions.New(githubactions.WithGetenv(func(key string) string {
switch key {
case "GITHUB_OUTPUT":
return os.Getenv(key)
case "INPUT_OVH-ENDPOINT":
return "http://" + s.Listener.Addr().String()
}
return "FAKEVALUE-" + key
}))
err := run(action)
if err != nil {
t.Fatal(err)
}
if os.Getenv("RUN_FOREVER") == "1" {
action.SetOutput("OVH_ENDPOINT", "http://"+s.Listener.Addr().String())
select {}
}
}