Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 20 additions & 0 deletions cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down Expand Up @@ -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"}},
Expand Down
11 changes: 11 additions & 0 deletions testdata/cli/autnum-text.golden
Original file line number Diff line number Diff line change
@@ -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 ---
5 changes: 5 additions & 0 deletions testdata/cli/autnum-whois-json.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
exit: 1
--- stdout ---

--- stderr ---
# Error: --whois output is only supported for domain queries
5 changes: 5 additions & 0 deletions testdata/cli/autnum-whois.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
exit: 1
--- stdout ---

--- stderr ---
# Error: --whois output is only supported for domain queries