diff --git a/cli.go b/cli.go index 9203ae6..484a923 100644 --- a/cli.go +++ b/cli.go @@ -542,6 +542,15 @@ func RunCLI(args []string, stdout io.Writer, stderr io.Writer, options CLIOption fmt.Fprintln(stderr, "") } + // WHOIS style output is only implemented for domains. Fail instead of + // printing nothing and exiting successfully for other object types. + if *outputFormatWhois { + if _, ok := resp.Object.(*Domain); !ok { + printError(stderr, "Error: --whois output is only supported for domain queries") + return 1 + } + } + // Output formatting. if !*outputFormatText && !*outputFormatWhois && !*outputFormatJSON && !*outputFormatRaw { *outputFormatText = true diff --git a/cli_test.go b/cli_test.go index 4444c7e..400bda4 100644 --- a/cli_test.go +++ b/cli_test.go @@ -33,12 +33,29 @@ func fixtureServer(t *testing.T) *httptest.Server { _, _ = w.Write(test.LoadFile("rdap/rdap.nic.cz/domain-example.cz.json")) }) + mux.HandleFunc("/autnum/64496", func(w http.ResponseWriter, _ *http.Request) { + w.Header().Set("Content-Type", "application/rdap+json") + _, _ = w.Write([]byte(autnumFixture)) + }) + srv := httptest.NewServer(mux) t.Cleanup(srv.Close) return srv } +// autnumFixture is a minimal autnum response (AS64496 is reserved for +// documentation, RFC 5398) used to exercise non-domain queries. +const autnumFixture = `{ + "rdapConformance": ["rdap_level_0"], + "objectClassName": "autnum", + "handle": "AS64496", + "startAutnum": 64496, + "endAutnum": 64496, + "name": "EXAMPLE-AS", + "type": "DIRECT ALLOCATION" +}` + func http2FixtureServer(t *testing.T) *httptest.Server { t.Helper() @@ -99,6 +116,9 @@ func TestRunCLIGolden(t *testing.T) { {"domain-json", withServer("--json", "example.cz")}, {"domain-whois", withServer("--whois", "example.cz")}, {"domain-raw", withServer("--raw", "example.cz")}, + {"autnum-text", withServer("AS64496")}, + {"autnum-whois", withServer("--whois", "AS64496")}, + {"autnum-whois-json", withServer("--whois", "--json", "AS64496")}, {"version", []string{"--version"}}, {"no-args", []string{}}, {"unknown-type", []string{"--type", "bogus", "example.cz"}}, diff --git a/testdata/cli/autnum-text.golden b/testdata/cli/autnum-text.golden new file mode 100644 index 0000000..238ce7c --- /dev/null +++ b/testdata/cli/autnum-text.golden @@ -0,0 +1,11 @@ +exit: 0 +--- stdout --- +Autnum: + Handle: AS64496 + Name: EXAMPLE-AS + Type: DIRECT ALLOCATION + StartAutnum: 64496 + EndAutnum: 64496 + Conformance: rdap_level_0 + +--- stderr --- diff --git a/testdata/cli/autnum-whois-json.golden b/testdata/cli/autnum-whois-json.golden new file mode 100644 index 0000000..ff27bf0 --- /dev/null +++ b/testdata/cli/autnum-whois-json.golden @@ -0,0 +1,5 @@ +exit: 1 +--- stdout --- + +--- stderr --- +# Error: --whois output is only supported for domain queries diff --git a/testdata/cli/autnum-whois.golden b/testdata/cli/autnum-whois.golden new file mode 100644 index 0000000..ff27bf0 --- /dev/null +++ b/testdata/cli/autnum-whois.golden @@ -0,0 +1,5 @@ +exit: 1 +--- stdout --- + +--- stderr --- +# Error: --whois output is only supported for domain queries