Compare commits

..
4 Commits
5 changed files with 96 additions and 7 deletions
+12 -4
View File
@@ -18,7 +18,11 @@ func (qe *QueryExecutor) executeQuery() ([]byte, error) {
}
for key, value := range qe.Headers {
httpRequest.Header.Set(key, value.(string))
strValue, ok := value.(string)
if !ok {
strValue = fmt.Sprintf("%v", value)
}
httpRequest.Header.Set(key, strValue)
}
var retries_max int
@@ -41,8 +45,13 @@ func (qe *QueryExecutor) executeQuery() ([]byte, error) {
qe.Logger.Debug("Error while executing http request", map[string]interface{}{"error": err.Error()})
return err
}
defer io.Copy(io.Discard, httpResponse.Body)
defer httpResponse.Body.Close()
defer func() {
_, err := io.Copy(io.Discard, httpResponse.Body)
if err != nil {
qe.Logger.Debug("Error while discarding http response body;", map[string]interface{}{"error": err.Error()})
}
httpResponse.Body.Close()
}()
if httpResponse.StatusCode < 200 || httpResponse.StatusCode >= 204 {
return fmt.Errorf("HTTP error - unacceptable status code: \"%s\" for \"%s\"", httpResponse.Status, httpRequest.URL)
@@ -59,7 +68,6 @@ func (qe *QueryExecutor) executeQuery() ([]byte, error) {
qe.Logger.Debug("Error while unmarshalling http response;", map[string]interface{}{"error": err.Error()})
return fmt.Errorf("Error while unmarshalling http response: %s", err.Error())
}
return nil
},
retry.OnRetry(func(n uint, err error) {
+64 -1
View File
@@ -1,6 +1,9 @@
package gql
import "testing"
import (
"reflect"
"testing"
)
func (suite *Tests) Test_searchForKeysInMapStringInterface() {
type args struct {
@@ -41,3 +44,63 @@ func (suite *Tests) Test_searchForKeysInMapStringInterface() {
})
}
}
func (suite *Tests) TestBaseClient_decodeResponse() {
type args struct {
response []byte
}
tests := []struct {
name string
args args
want any
wantErr bool
setType string
wantType string
}{
{
name: "TestBaseClient_decodeResponse_mapstring",
args: args{
response: []byte(`{"data":{"viewer":{"login":"lukaszraczylo"}}}`),
},
want: map[string]interface{}{
"data": map[string]interface{}{
"viewer": map[string]interface{}{
"login": "lukaszraczylo",
},
},
},
setType: "mapstring",
wantType: "map[string]interface {}",
},
{
name: "TestBaseClient_decodeResponse_string",
args: args{
response: []byte(`{"data":{"viewer":{"login":"lukaszraczylo"}}}`),
},
want: `{"data":{"viewer":{"login":"lukaszraczylo"}}}`,
setType: "string",
wantType: "string",
},
{
name: "TestBaseClient_decodeResponse_byte",
args: args{
response: []byte(`{"data":{"viewer":{"login":"lukaszraczylo"}}}`),
},
want: []byte(`{"data":{"viewer":{"login":"lukaszraczylo"}}}`),
setType: "byte",
wantType: "[]uint8",
},
}
for _, tt := range tests {
suite.T().Run(tt.name, func(t *testing.T) {
b := NewConnection()
b.SetOutput(tt.setType)
got, err := b.decodeResponse(tt.args.response)
if (err != nil) != tt.wantErr {
t.Errorf("decodeResponse() error = %v, wantErr %v", err, tt.wantErr)
return
}
assert.Equal(tt.wantType, reflect.TypeOf(got).String())
})
}
}
+5 -1
View File
@@ -11,14 +11,18 @@ import (
func (b *BaseClient) createHttpClient() (http_client *http.Client) {
httpTransport := &http.Transport{
MaxIdleConns: 10,
MaxIdleConns: 512,
MaxConnsPerHost: 512,
MaxIdleConnsPerHost: 512,
IdleConnTimeout: 15 * time.Second,
ResponseHeaderTimeout: 15 * time.Second,
DisableKeepAlives: false,
DisableCompression: false,
}
if strings.HasPrefix(b.endpoint, "http://") {
http_client = &http.Client{
Timeout: 15 * time.Second,
Transport: httpTransport,
CheckRedirect: func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
+1 -1
View File
@@ -11,7 +11,7 @@ import (
func NewConnection() (b *BaseClient) {
b = &BaseClient{
endpoint: envutil.Getenv("GRAPHQL_ENDPOINT", "https://api.github.com/graphql"),
responseType: envutil.Getenv("GRAPHQL_RESPONSE_TYPE", "string"),
responseType: envutil.Getenv("GRAPHQL_OUTPUT", "string"),
Logger: logging.NewLogger(),
cache: cache.New(time.Duration(envutil.GetInt("GRAPHQL_CACHE_TTL", 5)) * time.Second),
cache_global: envutil.GetBool("GRAPHQL_CACHE_ENABLED", false),
+14
View File
@@ -235,6 +235,20 @@ func (suite *Tests) TestBaseClient_Query() {
wantReturned_value: []byte(`{"__type":null}`),
wantErr: false,
},
{
name: "TestBaseClient_Query_tgbotapp_valid_query_string",
graphQLServer: "https://telegram-bot.app/v1/graphql",
wantType: "string",
args: args{
query: `query {
__type(name: "Query") {
name
}
}`,
},
wantReturned_value: string(`{"__type":null}`),
wantErr: false,
},
{
name: "TestBaseClient_Query_tgbotapp_valid_query_invalid_type",
graphQLServer: "https://telegram-bot.app/v1/graphql",