<commit_before>package httpclient

import (
	"bytes"
	"context"
	"fmt"
	"net/http"
	"net/url"
	"reflect"
	"strings"
	"testing"

	"github.com/golang/mock/gomock"
)

func TestNewContextCanWrapDefaultClient(t *testing.T) {
	clientContext := NewContext(http.DefaultClient)
	if _, ok := clientContext.(ClientContext); !ok {
		t.Error("http.DefaultClient can not be wrapped with ClientContext")
	}
}

func TestClientContextGetContextAddsContext(t *testing.T) {
	mockCtrl := gomock.NewController(t)
	defer mockCtrl.Finish()

	mockClient := NewMockClient(mockCtrl)
	client := NewContext(mockClient)

	ctx := context.TODO()
	expectedResp := &http.Response{}
	req, err := http.NewRequest("GET", "http://test.com", nil)
	if err != nil {
		panic(err)
	}
	req = req.WithContext(ctx)

	mockClient.EXPECT().Do(reqMatcher{req}).Return(expectedResp, nil)
	resp, err := client.GetContext(ctx, "http://test.com")
	if !reflect.DeepEqual(resp, expectedResp) {
		t.Errorf("Incorrect response;\n\texpected: %#v\n\t     got: %#v", expectedResp, resp)
	}

	if !reflect.DeepEqual(err, nil) {
		t.Errorf("Incorrect error;\n\texpected: %#v\n\t     got: %#v", nil, err)
	}
}

func TestClientContextPostAddsContext(t *testing.T) {
	mockCtrl := gomock.NewController(t)
	defer mockCtrl.Finish()

	mockClient := NewMockClient(mockCtrl)
	client := NewContext(mockClient)

	ctx := context.TODO()
	contentType := "text/html"
	expectedResp := &http.Response{}
	body := bytes.NewBufferString("")
	req, err := http.NewRequest("POST", "http://test.com", body)
	if err != nil {
		panic(err)
	}
	req.Header.Set("content-type", contentType)
	req = req.WithContext(ctx)

	mockClient.EXPECT().Do(reqMatcher{req}).Return(expectedResp, nil)
	resp, err := client.PostContext(ctx, "http://test.com", contentType, body)
	if !reflect.DeepEqual(resp, expectedResp) {
		t.Errorf("Incorrect response;\n\texpected: %#v\n\t     got: %#v", expectedResp, resp)
	}

	if !reflect.DeepEqual(err, nil) {
		t.Errorf("Incorrect error;\n\texpected: %#v\n\t     got: %#v", nil, err)
	}
}

func TestClientContextHeadAddsContext(t *testing.T) {
	mockCtrl := gomock.NewController(t)
	defer mockCtrl.Finish()

	mockClient := NewMockClient(mockCtrl)
	client := NewContext(mockClient)

	ctx := context.TODO()
	expectedResp := &http.Response{}
	req, err := http.NewRequest("HEAD", "http://test.com", nil)
	if err != nil {
		panic(err)
	}
	req = req.WithContext(ctx)

	mockClient.EXPECT().Do(reqMatcher{req}).Return(expectedResp, nil)
	resp, err := client.HeadContext(ctx, "http://test.com")
	if !reflect.DeepEqual(resp, expectedResp) {
		t.Errorf("Incorrect response;\n\texpected: %#v\n\t     got: %#v", expectedResp, resp)
	}

	if !reflect.DeepEqual(err, nil) {
		t.Errorf("Incorrect error;\n\texpected: %#v\n\t     got: %#v", nil, err)
	}
}

func TestClientContextPostFormAddsContext(t *testing.T) {
	mockCtrl := gomock.NewController(t)
	defer mockCtrl.Finish()

	mockClient := NewMockClient(mockCtrl)
	client := NewContext(mockClient)

	var values url.Values
	ctx := context.TODO()
	contentType := "application/x-www-form-urlencoded"
	expectedResp := &http.Response{}
	body := strings.NewReader(values.Encode())
	req, err := http.NewRequest("POST", "http://test.com", body)
	if err != nil {
		panic(err)
	}
	req.Header.Set("content-type", contentType)
	req = req.WithContext(ctx)

	mockClient.EXPECT().Do(reqMatcher{req}).Return(expectedResp, nil)
	resp, err := client.PostFormContext(ctx, "http://test.com", values)
	if !reflect.DeepEqual(resp, expectedResp) {
		t.Errorf("Incorrect response;\n\texpected: %#v\n\t     got: %#v", expectedResp, resp)
	}

	if !reflect.DeepEqual(err, nil) {
		t.Errorf("Incorrect error;\n\texpected: %#v\n\t     got: %#v", nil, err)
	}
}

type reqMatcher struct {
	x *http.Request
}

var _ gomock.Matcher = reqMatcher{}

func (m reqMatcher) Matches(i interface{}) bool {
	x, ok := i.(*http.Request)<commit_msg>Test for error cases in ClientContext methods<commit_after>package httpclient

import (
	"bytes"
	"context"
	"fmt"
	"net/http"
	"net/url"
	"reflect"
	"strings"
	"testing"

	"github.com/golang/mock/gomock"
)

type responseTestCase struct {
	returnResponse *http.Response
	returnErr      error
	expectedResp   *http.Response
	expectedErr    error
}

func TestNewContextCanWrapDefaultClient(t *testing.T) {
	clientContext := NewContext(http.DefaultClient)
	if _, ok := clientContext.(ClientContext); !ok {
		t.Error("http.DefaultClient can not be wrapped with ClientContext")
	}
}

func TestClientContextGetContextAddsContext(t *testing.T) {
	var testCases = map[string]responseTestCase{
		"returns response": {
			returnResponse: &http.Response{},
			expectedResp:   &http.Response{},
		},
		"returns error": {
			returnErr:   fmt.Errorf("error"),
			expectedErr: fmt.Errorf("error"),
		},
	}

	for testName, tt := range testCases {
		t.Run(testName, func(t *testing.T) {
			mockCtrl := gomock.NewController(t)
			defer mockCtrl.Finish()

			mockClient := NewMockClient(mockCtrl)
			client := NewContext(mockClient)

			ctx := context.TODO()
			req, err := http.NewRequest("GET", "http://test.com", nil)
			if err != nil {
				panic(err)
			}
			req = req.WithContext(ctx)

			mockClient.EXPECT().Do(reqMatcher{req}).Return(tt.expectedResp, tt.expectedErr)
			resp, err := client.GetContext(ctx, "http://test.com")
			if !reflect.DeepEqual(resp, tt.expectedResp) {
				t.Errorf("Incorrect response;\n\texpected: %#v\n\t     got: %#v", tt.expectedResp, resp)
			}

			if !reflect.DeepEqual(err, tt.expectedErr) {
				t.Errorf("Incorrect error;\n\texpected: %#v\n\t     got: %#v", tt.expectedErr, err)
			}
		})
	}
}

func TestClientContextPostAddsContext(t *testing.T) {
	var testCases = map[string]responseTestCase{
		"returns response": {
			returnResponse: &http.Response{},
			expectedResp:   &http.Response{},
		},
		"returns error": {
			returnErr:   fmt.Errorf("error"),
			expectedErr: fmt.Errorf("error"),
		},
	}

	for testName, tt := range testCases {
		t.Run(testName, func(t *testing.T) {
			mockCtrl := gomock.NewController(t)
			defer mockCtrl.Finish()

			mockClient := NewMockClient(mockCtrl)
			client := NewContext(mockClient)

			ctx := context.TODO()
			contentType := "text/html"
			body := bytes.NewBufferString("")
			req, err := http.NewRequest("POST", "http://test.com", body)
			if err != nil {
				panic(err)
			}
			req.Header.Set("content-type", contentType)
			req = req.WithContext(ctx)

			mockClient.EXPECT().Do(reqMatcher{req}).Return(tt.returnResponse, tt.returnErr)
			resp, err := client.PostContext(ctx, "http://test.com", contentType, body)
			if !reflect.DeepEqual(resp, tt.expectedResp) {
				t.Errorf("Incorrect response;\n\texpected: %#v\n\t     got: %#v", tt.expectedResp, resp)
			}

			if !reflect.DeepEqual(err, tt.expectedErr) {
				t.Errorf("Incorrect error;\n\texpected: %#v\n\t     got: %#v", tt.expectedErr, err)
			}
		})
	}
}

func TestClientContextHeadAddsContext(t *testing.T) {
	var testCases = map[string]responseTestCase{
		"returns response": {
			returnResponse: &http.Response{},
			expectedResp:   &http.Response{},
		},
		"returns error": {
			returnErr:   fmt.Errorf("error"),
			expectedErr: fmt.Errorf("error"),
		},
	}

	for testName, tt := range testCases {
		t.Run(testName, func(t *testing.T) {
			mockCtrl := gomock.NewController(t)
			defer mockCtrl.Finish()

			mockClient := NewMockClient(mockCtrl)
			client := NewContext(mockClient)

			ctx := context.TODO()
			req, err := http.NewRequest("HEAD", "http://test.com", nil)
			if err != nil {
				panic(err)
			}
			req = req.WithContext(ctx)

			mockClient.EXPECT().Do(reqMatcher{req}).Return(tt.returnResponse, tt.returnErr)
			resp, err := client.HeadContext(ctx, "http://test.com")
			if !reflect.DeepEqual(resp, tt.expectedResp) {
				t.Errorf("Incorrect response;\n\texpected: %#v\n\t     got: %#v", tt.expectedResp, resp)
			}

			if !reflect.DeepEqual(err, tt.expectedErr) {
				t.Errorf("Incorrect error;\n\texpected: %#v\n\t     got: %#v", tt.expectedErr, err)
			}
		})
	}
}

func TestClientContextPostFormAddsContext(t *testing.T) {
	var testCases = map[string]responseTestCase{
		"returns response": {
			returnResponse: &http.Response{},
			expectedResp:   &http.Response{},
		},
		"returns error": {
			returnErr:   fmt.Errorf("error"),
			expectedErr: fmt.Errorf("error"),
		},
	}

	for testName, tt := range testCases {
		t.Run(testName, func(t *testing.T) {
			mockCtrl := gomock.NewController(t)
			defer mockCtrl.Finish()

			mockClient := NewMockClient(mockCtrl)
			client := NewContext(mockClient)

			var values url.Values
			ctx := context.TODO()
			contentType := "application/x-www-form-urlencoded"
			body := strings.NewReader(values.Encode())
			req, err := http.NewRequest("POST", "http://test.com", body)
			if err != nil {
				panic(err)
			}
			req.Header.Set("content-type", contentType)
			req = req.WithContext(ctx)

			mockClient.EXPECT().Do(reqMatcher{req}).Return(tt.returnResponse, tt.returnErr)
			resp, err := client.PostFormContext(ctx, "http://test.com", values)
			if !reflect.DeepEqual(resp, tt.expectedResp) {
				t.Errorf("Incorrect response;\n\texpected: %#v\n\t     got: %#v", tt.expectedResp, resp)
			}

			if !reflect.DeepEqual(err, tt.expectedErr) {
				t.Errorf("Incorrect error;\n\texpected: %#v\n\t     got: %#v", tt.expectedErr, err)
			}
		})
	}
}

type reqMatcher struct {
	x *http.Request
}

var _ gomock.Matcher = reqMatcher{}

func (m reqMatcher) Matches(i interface{}) bool {
	x, ok := i.(*http.Request)
{'fraction_non_alphanumeric': 0.1653276955602537, 'fraction_numerical': 0.0, 'mean_word_length': 4.155858310626703, 'pattern_counts': {'":': 8, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '5167295', 'n_tokens_mistral': 3658, 'n_tokens_neox': 3344, 'n_words': 642}