<commit_before>package git

import (
	"bytes"
	"net/http"
	"net/http/httptest"
	"testing"
)

func TestGogsDeployPush(t *testing.T) {
	repo := &Repo{Branch: "master", Hook: HookConfig{Url: "/gogs_deploy"}}
	ghHook := GogsHook{}

	for i, test := range []struct {
		body         string
		event        string
		responseBody string
		code         int
	}{
		{"", "", "", 400},
		{"", "push", "", 400},
		{pushBodyOther, "push", "", 200},
		{pushBodyPartial, "push", "", 400},
		{"", "ping", "pong", 200},
	} {

		req, err := http.NewRequest("POST", "/gogs_deploy", bytes.NewBuffer([]byte(test.body)))
		if err != nil {
			t.Fatalf("Test %v: Could not create HTTP request: %v", i, err)
		}

		if test.event != "" {
			req.Header.Add("X-Gogs-Event", test.event)
		}

		rec := httptest.NewRecorder()

		code, err := ghHook.Handle(rec, req, repo)

		if code != test.code {
			t.Errorf("Test %d: Expected response code to be %d but was %d", i, test.code, code)
		}

		if rec.Body.String() != test.responseBody {
			t.Errorf("Test %d: Expected response body to be '%v' but was '%v'", i, test.responseBody, rec.Body.String())
		}
	}

}

var pushBodyPartial = `
{
  "ref": ""
}
`

var pushBodyOther = `
{
  "ref": "refs/heads/some-other-branch"
}
`<commit_msg>fix gogs test<commit_after>package git

import (
	"bytes"
	"net/http"
	"net/http/httptest"
	"testing"
)

func TestGogsDeployPush(t *testing.T) {
	repo := &Repo{Branch: "master", Hook: HookConfig{Url: "/gogs_deploy"}}
	gsHook := GogsHook{}

	for i, test := range []struct {
		body         string
		event        string
		responseBody string
		code         int
	}{
		{"", "", "", 400},
		{"", "push", "", 400},
		{pushGSBodyOther, "push", "", 200},
		{pushGSBodyPartial, "push", "", 400},
		{"", "ping", "pong", 200},
	} {

		req, err := http.NewRequest("POST", "/gogs_deploy", bytes.NewBuffer([]byte(test.body)))
		if err != nil {
			t.Fatalf("Test %v: Could not create HTTP request: %v", i, err)
		}

		if test.event != "" {
			req.Header.Add("X-Gogs-Event", test.event)
		}

		rec := httptest.NewRecorder()

		code, err := gsHook.Handle(rec, req, repo)

		if code != test.code {
			t.Errorf("Test %d: Expected response code to be %d but was %d", i, test.code, code)
		}

		if rec.Body.String() != test.responseBody {
			t.Errorf("Test %d: Expected response body to be '%v' but was '%v'", i, test.responseBody, rec.Body.String())
		}
	}

}

var pushGSBodyPartial = `
{
  "ref": ""
}
`

var pushGSBodyOther = `
{
  "ref": "refs/heads/some-other-branch"
}
`
{'fraction_non_alphanumeric': 0.1976, 'fraction_numerical': 0.012, 'mean_word_length': 3.2606473594548553, 'pattern_counts': {'":': 4, '<': 3, '<?xml version=': 0, '>': 3, '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': '1827552', 'n_tokens_mistral': 1052, 'n_tokens_neox': 952, 'n_words': 228}