// Copyright 2020-2026 The happyDomain Authors // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package server import ( "context" "errors" "net/http" "net/http/httptest" "net/url" "strings" "testing" "git.happydns.org/checker-sdk-go/checker" ) // interactiveProvider embeds testProvider and adds Interactive. type interactiveProvider struct { *testProvider fields []checker.CheckerOptionField parseFn func(r *http.Request) (checker.CheckerOptions, error) parseErr error } func (p *interactiveProvider) RenderForm() []checker.CheckerOptionField { return p.fields } func (p *interactiveProvider) ParseForm(r *http.Request) (checker.CheckerOptions, error) { if p.parseErr != nil { return nil, p.parseErr } if p.parseFn != nil { return p.parseFn(r) } return checker.CheckerOptions{"domain": r.FormValue("domain")}, nil } func postForm(handler http.Handler, path string, values url.Values) *httptest.ResponseRecorder { req := httptest.NewRequest("POST", path, strings.NewReader(values.Encode())) req.Header.Set("Content-Type", "application/x-www-form-urlencoded") rec := httptest.NewRecorder() handler.ServeHTTP(rec, req) return rec } // minimalProvider implements only ObservationProvider. type minimalProvider struct{ key checker.ObservationKey } func (m *minimalProvider) Key() checker.ObservationKey { return m.key } func (m *minimalProvider) Collect(ctx context.Context, opts checker.CheckerOptions) (any, error) { return nil, nil } func TestCheck_NotRegistered_WhenProviderLacksInterface(t *testing.T) { p := &minimalProvider{key: "test"} srv := New(p) defer srv.Close() rec := doRequest(srv.Handler(), "GET", "/check", nil, nil) if rec.Code != http.StatusNotFound { t.Fatalf("GET /check without Interactive = %d, want 404", rec.Code) } } func TestCheck_Form_Renders(t *testing.T) { p := &interactiveProvider{ testProvider: &testProvider{key: "test"}, fields: []checker.CheckerOptionField{ {Id: "domain", Type: "string", Label: "Domain name", Required: true, Placeholder: "example.com"}, {Id: "verbose", Type: "bool", Label: "Verbose", Default: true}, {Id: "flavor", Type: "string", Choices: []string{"a", "b"}, Default: "b"}, {Id: "hidden", Type: "string", Hide: true}, }, } srv := New(p) defer srv.Close() rec := doRequest(srv.Handler(), "GET", "/check", nil, nil) if rec.Code != http.StatusOK { t.Fatalf("GET /check = %d, want 200", rec.Code) } body := rec.Body.String() for _, want := range []string{ `name="domain"`, `placeholder="example.com"`, `Domain name`, `type="checkbox"`, `name="verbose"`, ` checked`, `