<commit_before>// Copyright (c) 2016, <NAME> <<EMAIL>>
// All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package apiClient

import (
	"context"
	"encoding/json"
	"io/ioutil"
	"net/http"
	"net/http/httptest"
	"net/url"
	"strings"
	"testing"
	"time"
)

type TestResponseJSON struct {
	Method  string
	URL     *url.URL
	Headers map[string][]string
	Body    string
}

func TestClient(t *testing.T) {
	contentType := "application/json; charset=utf-8"
	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		switch r.URL.Path {
		case "/empty":
		case "/test.html":
			w.Header().Set("Content-Type", "text/html")
			_, _ = w.Write([]byte(`Test`))
		case "/syntax-error.json":
			w.Header().Set("Content-Type", contentType)
			_, _ = w.Write([]byte(`{1}`))
		case "/type-error.json":
			w.Header().Set("Content-Type", contentType)
			_, _ = w.Write([]byte(`{"method":123}`))
		case "/bad-request":
			w.WriteHeader(http.StatusBadRequest)
			_, _ = w.Write([]byte(`Bad data...`))
		case "/internal-server-error":
			w.WriteHeader(http.StatusInternalServerError)
		case "/entity-not-found":
			w.Header().Set("Content-Type", contentType)
			w.WriteHeader(http.StatusNotFound)
			_, _ = w.Write([]byte(`{"message":"entity not found"}`))
		case "/error-invalid-message-type":
			w.Header().Set("Content-Type", contentType)
			w.WriteHeader(http.StatusNotFound)
			_, _ = w.Write([]byte(`{"message":1}`))
		case "/json-syntax-error":
			w.Header().Set("Content-Type", contentType)
			w.WriteHeader(http.StatusNotFound)
			_, _ = w.Write([]byte(`{1}`))
		case "/error-1000":
			w.Header().Set("Content-Type", contentType)
			w.WriteHeader(http.StatusBadRequest)
			_, _ = w.Write([]byte(`{"code":1000}`))
		case "/2s-request":
			time.Sleep(2 * time.Second)
			w.Header().Set("Content-Type", contentType)
			w.WriteHeader(http.StatusOK)
			_, _ = w.Write([]byte(`{"code":200}`))
		default:
			body, err := ioutil.ReadAll(r.Body)
			if err != nil {
				t.Error(err)
			}
			r.Body.Close()
			b, err := json.Marshal(TestResponseJSON{
				Method:  r.Method,
				URL:     r.URL,
				Headers: r.Header,
				Body:    string(body),
			})
			if err != nil {
				t.Error(err)
			}
			w.Header().Set("Content-Type", contentType)
			_, _ = w.Write(b)
		}
	}))
	defer ts.Close()

	t.Run("Basic", func(t *testing.T) {
		client := New(ts.URL, nil)
		r, err := client.Request("GET", "/", nil, nil, nil)
		if err != nil {
			t.Error(err.Error())
		}
		if r == nil {
			t.Error("response must not be nil")
		}
		if err := client.JSON("GET", "/", nil, nil, nil); err != nil {
			t.Error(err.Error())
		}
		data, ct, err := client.Stream("GET", "/", nil, nil, nil)
		if err != nil {
			t.Error(err.Error())
		}
		b, err := ioutil.ReadAll(data)
		if err != nil {
			t.Error(err.Error())
		}
		data.Close()
		body := `{"Method":"GET","URL":{"Scheme":"","Opaque":"","User":null,"Host":"","Path":"/","RawPath":"","ForceQuery":false,"RawQuery":"","Fragment":""},"Headers":{"Accept-Encoding":["gzip"],"User-Agent":["Go-http-client/1.1"]},"Body":""}`
		if string(b) != body {
			t.Errorf("expected body %q, got %q", body, string(b))
		}
		if ct != contentType {
			t.Errorf("expected Content-Type header %q, got %q", contentType, ct)
		}
	})

	t.Run("EndpointWithoutScheme", func(t *testing.T) {
		client := New(ts.URL[7:], nil)
		if _, err := client.Request("GET", "/", nil, nil, nil); err != nil {
			t.Error(err.Error())
		}
		if err := client.JSON("GET", "/", nil, nil, nil); err != nil {
			t.Error(err.Error())
		}
		data, ct, err := client.Stream("GET", "/", nil, nil, nil)
		if err != nil {
			t.Error(err.Error())
		}
		b, err := ioutil.ReadAll(data)
		if err != nil {
			t.Error(err.Error())
		}
		data.Close()
		body := `{"Method":"GET","URL":{"Scheme":"","Opaque":"","User":null,"Host":"","Path":"/","RawPath":"","ForceQuery":false,"RawQuery":"","Fragment":""},"Headers":{"Accept-Encoding":["gzip"],"User-Agent":["Go-http-client/1.1"]},"Body":""}`
		if string(b) != body {
			t.Errorf("expected body %q, got %q", body, string(b))
		}
		if ct != contentType {
			t.Errorf("expected Content-Type header %q, got %q", contentType, ct)
		}
	})

	t.Run("EndpointInvalidEndpoint", func(t *testing.T) {
		client := New("", nil)
		want := "Get http:///: http: no Host in request URL"
		if _, err := client.Request("GET", "/", nil, nil, nil); err.Error() != want {
			t.Errorf("expected error %q, got %v", want, err)
		}
		if err := client.JSON("GET", "/", nil, nil, nil); err.Error() != want {
			t.Errorf("expected error %q, got %v", want, err)
		}
		if _, _, err := client.Stream("GET", "/", nil, nil, nil); err.Error() != want {
			t.Errorf("expected error %q, got %v", want, err)
		}
	})

	t.Run("DefaultHTTPClient", func(t *testing.T) {
		client := New(ts.URL, nil)
		client.HTTPClient = nil
		if _, err := client.Request("GET", "/", nil, nil, nil); err != nil {
			t.Error(err.Error())
		}
		if err := client.JSON("GET", "/", nil, nil, nil); err != nil {
			t.Error(err.Error())
		}
		data, ct, err := client.Stream("GET", "/", nil, nil, nil)
		if err != nil {
			t.Error(err.Error())
		}
		b, err := ioutil.ReadAll(data)
		if err != nil {
			t.Error(err.Error())<commit_msg>Update client/api test EndpointInvalidEndpoint<commit_after>// Copyright (c) 2016, <NAME> <<EMAIL>>
// All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package apiClient

import (
	"context"
	"encoding/json"
	"fmt"
	"io/ioutil"
	"net/http"
	"net/http/httptest"
	"net/url"
	"strings"
	"testing"
	"time"
)

type TestResponseJSON struct {
	Method  string
	URL     *url.URL
	Headers map[string][]string
	Body    string
}

func TestClient(t *testing.T) {
	contentType := "application/json; charset=utf-8"
	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		switch r.URL.Path {
		case "/empty":
		case "/test.html":
			w.Header().Set("Content-Type", "text/html")
			_, _ = w.Write([]byte(`Test`))
		case "/syntax-error.json":
			w.Header().Set("Content-Type", contentType)
			_, _ = w.Write([]byte(`{1}`))
		case "/type-error.json":
			w.Header().Set("Content-Type", contentType)
			_, _ = w.Write([]byte(`{"method":123}`))
		case "/bad-request":
			w.WriteHeader(http.StatusBadRequest)
			_, _ = w.Write([]byte(`Bad data...`))
		case "/internal-server-error":
			w.WriteHeader(http.StatusInternalServerError)
		case "/entity-not-found":
			w.Header().Set("Content-Type", contentType)
			w.WriteHeader(http.StatusNotFound)
			_, _ = w.Write([]byte(`{"message":"entity not found"}`))
		case "/error-invalid-message-type":
			w.Header().Set("Content-Type", contentType)
			w.WriteHeader(http.StatusNotFound)
			_, _ = w.Write([]byte(`{"message":1}`))
		case "/json-syntax-error":
			w.Header().Set("Content-Type", contentType)
			w.WriteHeader(http.StatusNotFound)
			_, _ = w.Write([]byte(`{1}`))
		case "/error-1000":
			w.Header().Set("Content-Type", contentType)
			w.WriteHeader(http.StatusBadRequest)
			_, _ = w.Write([]byte(`{"code":1000}`))
		case "/2s-request":
			time.Sleep(2 * time.Second)
			w.Header().Set("Content-Type", contentType)
			w.WriteHeader(http.StatusOK)
			_, _ = w.Write([]byte(`{"code":200}`))
		default:
			body, err := ioutil.ReadAll(r.Body)
			if err != nil {
				t.Error(err)
			}
			r.Body.Close()
			b, err := json.Marshal(TestResponseJSON{
				Method:  r.Method,
				URL:     r.URL,
				Headers: r.Header,
				Body:    string(body),
			})
			if err != nil {
				t.Error(err)
			}
			w.Header().Set("Content-Type", contentType)
			_, _ = w.Write(b)
		}
	}))
	defer ts.Close()

	t.Run("Basic", func(t *testing.T) {
		client := New(ts.URL, nil)
		r, err := client.Request("GET", "/", nil, nil, nil)
		if err != nil {
			t.Error(err.Error())
		}
		if r == nil {
			t.Error("response must not be nil")
		}
		if err := client.JSON("GET", "/", nil, nil, nil); err != nil {
			t.Error(err.Error())
		}
		data, ct, err := client.Stream("GET", "/", nil, nil, nil)
		if err != nil {
			t.Error(err.Error())
		}
		b, err := ioutil.ReadAll(data)
		if err != nil {
			t.Error(err.Error())
		}
		data.Close()
		body := `{"Method":"GET","URL":{"Scheme":"","Opaque":"","User":null,"Host":"","Path":"/","RawPath":"","ForceQuery":false,"RawQuery":"","Fragment":""},"Headers":{"Accept-Encoding":["gzip"],"User-Agent":["Go-http-client/1.1"]},"Body":""}`
		if string(b) != body {
			t.Errorf("expected body %q, got %q", body, string(b))
		}
		if ct != contentType {
			t.Errorf("expected Content-Type header %q, got %q", contentType, ct)
		}
	})

	t.Run("EndpointWithoutScheme", func(t *testing.T) {
		client := New(ts.URL[7:], nil)
		if _, err := client.Request("GET", "/", nil, nil, nil); err != nil {
			t.Error(err.Error())
		}
		if err := client.JSON("GET", "/", nil, nil, nil); err != nil {
			t.Error(err.Error())
		}
		data, ct, err := client.Stream("GET", "/", nil, nil, nil)
		if err != nil {
			t.Error(err.Error())
		}
		b, err := ioutil.ReadAll(data)
		if err != nil {
			t.Error(err.Error())
		}
		data.Close()
		body := `{"Method":"GET","URL":{"Scheme":"","Opaque":"","User":null,"Host":"","Path":"/","RawPath":"","ForceQuery":false,"RawQuery":"","Fragment":""},"Headers":{"Accept-Encoding":["gzip"],"User-Agent":["Go-http-client/1.1"]},"Body":""}`
		if string(b) != body {
			t.Errorf("expected body %q, got %q", body, string(b))
		}
		if ct != contentType {
			t.Errorf("expected Content-Type header %q, got %q", contentType, ct)
		}
	})

	t.Run("EndpointInvalidEndpoint", func(t *testing.T) {
		client := New("", nil)
		want := "http: no Host in request URL"
		if _, err := client.Request("GET", "/", nil, nil, nil); !strings.Contains(err.Error(), want) {
			fmt.Printf("%#v\n", err)
			t.Errorf("expected error %q, got %v", want, err)
		}
		if err := client.JSON("GET", "/", nil, nil, nil); !strings.Contains(err.Error(), want) {
			t.Errorf("expected error %q, got %v", want, err)
		}
		if _, _, err := client.Stream("GET", "/", nil, nil, nil); !strings.Contains(err.Error(), want) {
			t.Errorf("expected error %q, got %v", want, err)
		}
	})

	t.Run("DefaultHTTPClient", func(t *testing.T) {
		client := New(ts.URL, nil)
		client.HTTPClient = nil
		if _, err := client.Request("GET", "/", nil, nil, nil); err != nil {
			t.Error(err.Error())
		}
		if err := client.JSON("GET", "/", nil, nil, nil); err != nil {
			t.Error(err.Error())
		}
		data, ct, err := client.Stream("GET", "/", nil, nil, nil)
		if err != nil {
			t.Error(err.Error())
		}
		b, err := ioutil.ReadAll(data)
		if err != nil {
			t.Error(err.Error())
{'fraction_non_alphanumeric': 0.22889411764705883, 'fraction_numerical': 0.005458823529411765, 'mean_word_length': 3.5980095196884467, 'pattern_counts': {'":': 92, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7194362', 'n_tokens_mistral': 4390, 'n_tokens_neox': 3927, 'n_words': 797}