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
93 changes: 93 additions & 0 deletions hugegraph-client-go/api/api_response_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with this
* work for additional information regarding copyright ownership. The ASF
* licenses this file to You 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 api

import (
"errors"
"io"
"net/http"
"strings"
"testing"
)

func TestResponseStatusAndIsError(t *testing.T) {
tests := []struct {
code int
status string
isError bool
}{
{200, "200 OK", false},
{201, "201 Created", false},
{299, "299 ", false},
{300, "300 Multiple Choices", true},
{404, "404 Not Found", true},
{500, "500 Internal Server Error", true},
}
for _, tt := range tests {
r := &Response{StatusCode: tt.code}
if r.Status() != tt.status {
t.Errorf("Status() = %q, want %q", r.Status(), tt.status)
}
if r.IsError() != tt.isError {
t.Errorf("IsError() for %d = %v", tt.code, r.IsError())
}
}
var nilResponse *Response
if nilResponse.Status() != "" {
t.Errorf("nil Status() = %q", nilResponse.Status())
}
}

func TestResponseStringKeepsBody(t *testing.T) {
r := &Response{
StatusCode: http.StatusNotFound,
Body: io.NopCloser(strings.NewReader(`{"message":"missing"}`)),
}

if s := r.String(); s != `[404 Not Found] {"message":"missing"}` {
t.Errorf("String() = %q", s)
}
body, _ := io.ReadAll(r.Body)
if string(body) != `{"message":"missing"}` {
t.Errorf("body after String() = %q", body)
}
}

func TestResponseStringWithoutBody(t *testing.T) {
if s := (&Response{StatusCode: http.StatusNoContent}).String(); s != "[204 No Content] " {
t.Errorf("String() = %q", s)
}
var r *Response
if s := r.String(); s != "[0 <nil>]" {
t.Errorf("nil String() = %q", s)
}
}

func TestResponseStringWithBrokenBody(t *testing.T) {
r := &Response{StatusCode: http.StatusOK, Body: io.NopCloser(brokenReader{})}

if s := r.String(); s != "<error reading response body: broken>" {
t.Errorf("String() = %q", s)
}
}

type brokenReader struct{}

func (brokenReader) Read([]byte) (int, error) {
return 0, errors.New("broken")
}
110 changes: 110 additions & 0 deletions hugegraph-client-go/api/v1/version_fault_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with this
* work for additional information regarding copyright ownership. The ASF
* licenses this file to You 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 v1_test

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

hugegraph "github.com/apache/hugegraph-toolchain/hugegraph-client-go"
)

func fakeServerClient(t *testing.T, handler http.HandlerFunc) *hugegraph.CommonClient {
t.Helper()
server := httptest.NewServer(handler)
t.Cleanup(server.Close)
host, portString, err := net.SplitHostPort(server.Listener.Addr().String())
if err != nil {
t.Fatal(err)
}
port, err := strconv.Atoi(portString)
if err != nil {
t.Fatal(err)
}
client, err := hugegraph.NewCommonClient(hugegraph.Config{
Host: host, Port: port, Graph: "hugegraph", Username: "admin", Password: "pa",
})
if err != nil {
t.Fatal(err)
}
return client
}

func TestVersionWithFakeServer(t *testing.T) {
client := fakeServerClient(t, func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/versions" {
t.Errorf("path = %q", r.URL.Path)
}
_, _ = w.Write([]byte(`{"versions":{"version":"v1","core":"1.8.0",` +
`"gremlin":"3.8.1","api":"0.72.0.0"}}`))
})

resp, err := client.Version()
if err != nil {
t.Fatal(err)
}
if resp.StatusCode != http.StatusOK || resp.Versions.Core != "1.8.0" ||
resp.Versions.API != "0.72.0.0" || resp.Versions.Gremlin != "3.8.1" {
t.Errorf("response = %d %+v", resp.StatusCode, resp.Versions)
}
}

func TestVersionWithNonJSONBody(t *testing.T) {
client := fakeServerClient(t, func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusBadGateway)
_, _ = w.Write([]byte("<html>502 Bad Gateway</html>"))
})

resp, err := client.Version()
if err == nil {
t.Fatalf("non-json body must fail, got %+v", resp)
}
}

func TestVersionWithServerErrorJSON(t *testing.T) {
client := fakeServerClient(t, func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusInternalServerError)
_, _ = w.Write([]byte(`{"exception":"java.lang.IllegalStateException",` +
`"message":"backend unavailable"}`))
})

resp, err := client.Version()
if err != nil {
t.Fatal(err)
}
// The status code is kept for callers to detect the failure
if resp.StatusCode != http.StatusInternalServerError || resp.Versions.Core != "" {
t.Errorf("response = %d %+v", resp.StatusCode, resp.Versions)
}
}

func TestVersionConnectionDropped(t *testing.T) {
client := fakeServerClient(t, func(w http.ResponseWriter, r *http.Request) {
conn, _, err := w.(http.Hijacker).Hijack()
if err == nil {
conn.Close()
}
})

if resp, err := client.Version(); err == nil {
t.Fatalf("dropped connection must fail, got %+v", resp)
}
}
Loading
Loading