<commit_before>package octokit

import (
	"net/http"
	"testing"

	"github.com/stretchr/testify/assert"
)

func TestResponseError_empty_body(t *testing.T) {
	setup()
	defer tearDown()

	mux.HandleFunc("/error", func(w http.ResponseWriter, r *http.Request) {
		head := w.Header()
		head.Set("Content-Type", "application/json")
		w.WriteHeader(http.StatusBadRequest)
		respondWith(w, "")
	})

	req, _ := client.NewRequest("error")
	_, err := req.Get(nil)
	assert.Contains(t, err.Error(), "400 - Problems parsing error message: EOF")

	e := err.(*ResponseError)
	assert.Equal(t, ErrorBadRequest, e.Type)
}

func TestResponseError_Error_400(t *testing.T) {
	setup()
	defer tearDown()

	mux.HandleFunc("/error", func(w http.ResponseWriter, r *http.Request) {
		head := w.Header()
		head.Set("Content-Type", "application/json")
		w.WriteHeader(http.StatusBadRequest)
		respondWith(w, `{"message":"Problems parsing JSON"}`)
	})

	req, _ := client.NewRequest("error")
	_, err := req.Get(nil)
	assert.Contains(t, err.Error(), "400 - Problems parsing JSON")

	e := err.(*ResponseError)
	assert.Equal(t, ErrorBadRequest, e.Type)
}

func TestResponseError_Error_401(t *testing.T) {
	setup()
	defer tearDown()

	mux.HandleFunc("/error", func(w http.ResponseWriter, r *http.Request) {
		head := w.Header()
		head.Set("Content-Type", "application/json")
		w.WriteHeader(http.StatusUnauthorized)
		respondWith(w, `{"message":"Unauthorized"}`)
	})

	req, _ := client.NewRequest("error")
	_, err := req.Get(nil)
	assert.Contains(t, err.Error(), "401 - Unauthorized")

	e := err.(*ResponseError)
	assert.Equal(t, ErrorUnauthorized, e.Type)

	mux.HandleFunc("/error_2fa", func(w http.ResponseWriter, r *http.Request) {
		head := w.Header()
		head.Set("Content-Type", "application/json")
		head.Set("X-GitHub-OTP", "required; app")
		w.WriteHeader(http.StatusUnauthorized)
		respondWith(w, `{"message":"Unauthorized"}`)
	})

	req, _ = client.NewRequest("error_2fa")
	_, err = req.Get(nil)
	assert.Contains(t, err.Error(), "401 - Unauthorized")

	e = err.(*ResponseError)
	assert.Equal(t, ErrorOneTimePasswordRequired, e.Type)
}

func TestResponseError_Error_422_error(t *testing.T) {
	setup()
	defer tearDown()

	mux.HandleFunc("/error", func(w http.ResponseWriter, r *http.Request) {
		head := w.Header()
		head.Set("Content-Type", "application/json")
		w.WriteHeader(422)
		respondWith(w, `{"error":"No repository found for hubtopic"}`)
	})

	req, _ := client.NewRequest("error")
	_, err := req.Get(nil)
	assert.Contains(t, err.Error(), "Error: No repository found for hubtopic")

	e := err.(*ResponseError)
	assert.Equal(t, ErrorUnprocessableEntity, e.Type)
}

func TestResponseError_Error_422_error_summary(t *testing.T) {
	setup()
	defer tearDown()

	mux.HandleFunc("/error", func(w http.ResponseWriter, r *http.Request) {
		head := w.Header()
		head.Set("Content-Type", "application/json")
		w.WriteHeader(422)
		respondWith(w, `{"message":"Validation Failed", "errors": [{"resource":"Issue", "field": "title", "code": "missing_field"}]}`)
	})

	req, _ := client.NewRequest("error")
	_, err := req.Get(nil)
	assert.Contains(t, err.Error(), "422 - Validation Failed")
	assert.Contains(t, err.Error(), "missing_field error caused by title field on Issue resource")

	e := err.(*ResponseError)
	assert.Equal(t, ErrorUnprocessableEntity, e.Type)
}

func TestResponseError_Error_415(t *testing.T) {
	setup()
	defer tearDown()

	mux.HandleFunc("/error", func(w http.ResponseWriter, r *http.Request) {
		head := w.Header()
		head.Set("Content-Type", "application/json")
		w.WriteHeader(http.StatusUnsupportedMediaType)
		respondWith(w, `{"message":"Unsupported Media Type", "documentation_url":"http://developer.github.com/v3"}`)
	})

	req, _ := client.NewRequest("error")
	_, err := req.Get(nil)
	assert.Contains(t, err.Error(), "415 - Unsupported Media Type")
	assert.Contains(t, err.Error(), "// See: http://developer.github.com/v3")

	e := err.(*ResponseError)
	assert.Equal(t, ErrorUnsupportedMediaType, e.Type)
}

func TestResponseError_403(t *testing.T) {
	setup()
	defer tearDown()

	mux.HandleFunc("/error", func(w http.ResponseWriter, r *http.Request) {
		head := w.Header()
		head.Set("Content-Type", "application/json")
		w.WriteHeader(http.StatusForbidden)
		respondWith(w, `{"message":"API rate limit exceeded for xxx.xxx.xxx.xxx. (But here's the good news: Authenticated requests get a higher rate limit. Check out the documentation for more details.)", "documentation_url":"https://developer.github.com/v3/#rate-limiting"`)
	})

	req, _ := client.NewRequest("error")
	_, err := req.Get(nil)
	assert.Contains(t, err.Error(), "403 - Unsupported Media Type")
	assert.Contains(t, err.Error(), "// See: http://developer.github.com/v3")

	e := err.(*ResponseError)
	assert.Equal(t, ErrorUnsupportedMediaType, e.Type)
}<commit_msg>Fix test for HTTP 403 due to malformed JSON<commit_after>package octokit

import (
	"net/http"
	"testing"

	"github.com/stretchr/testify/assert"
)

func TestResponseError_empty_body(t *testing.T) {
	setup()
	defer tearDown()

	mux.HandleFunc("/error", func(w http.ResponseWriter, r *http.Request) {
		head := w.Header()
		head.Set("Content-Type", "application/json")
		w.WriteHeader(http.StatusBadRequest)
		respondWith(w, "")
	})

	req, _ := client.NewRequest("error")
	_, err := req.Get(nil)
	assert.Contains(t, err.Error(), "400 - Problems parsing error message: EOF")

	e := err.(*ResponseError)
	assert.Equal(t, ErrorBadRequest, e.Type)
}

func TestResponseError_Error_400(t *testing.T) {
	setup()
	defer tearDown()

	mux.HandleFunc("/error", func(w http.ResponseWriter, r *http.Request) {
		head := w.Header()
		head.Set("Content-Type", "application/json")
		w.WriteHeader(http.StatusBadRequest)
		respondWith(w, `{"message":"Problems parsing JSON"}`)
	})

	req, _ := client.NewRequest("error")
	_, err := req.Get(nil)
	assert.Contains(t, err.Error(), "400 - Problems parsing JSON")

	e := err.(*ResponseError)
	assert.Equal(t, ErrorBadRequest, e.Type)
}

func TestResponseError_Error_401(t *testing.T) {
	setup()
	defer tearDown()

	mux.HandleFunc("/error", func(w http.ResponseWriter, r *http.Request) {
		head := w.Header()
		head.Set("Content-Type", "application/json")
		w.WriteHeader(http.StatusUnauthorized)
		respondWith(w, `{"message":"Unauthorized"}`)
	})

	req, _ := client.NewRequest("error")
	_, err := req.Get(nil)
	assert.Contains(t, err.Error(), "401 - Unauthorized")

	e := err.(*ResponseError)
	assert.Equal(t, ErrorUnauthorized, e.Type)

	mux.HandleFunc("/error_2fa", func(w http.ResponseWriter, r *http.Request) {
		head := w.Header()
		head.Set("Content-Type", "application/json")
		head.Set("X-GitHub-OTP", "required; app")
		w.WriteHeader(http.StatusUnauthorized)
		respondWith(w, `{"message":"Unauthorized"}`)
	})

	req, _ = client.NewRequest("error_2fa")
	_, err = req.Get(nil)
	assert.Contains(t, err.Error(), "401 - Unauthorized")

	e = err.(*ResponseError)
	assert.Equal(t, ErrorOneTimePasswordRequired, e.Type)
}

func TestResponseError_Error_422_error(t *testing.T) {
	setup()
	defer tearDown()

	mux.HandleFunc("/error", func(w http.ResponseWriter, r *http.Request) {
		head := w.Header()
		head.Set("Content-Type", "application/json")
		w.WriteHeader(422)
		respondWith(w, `{"error":"No repository found for hubtopic"}`)
	})

	req, _ := client.NewRequest("error")
	_, err := req.Get(nil)
	assert.Contains(t, err.Error(), "Error: No repository found for hubtopic")

	e := err.(*ResponseError)
	assert.Equal(t, ErrorUnprocessableEntity, e.Type)
}

func TestResponseError_Error_422_error_summary(t *testing.T) {
	setup()
	defer tearDown()

	mux.HandleFunc("/error", func(w http.ResponseWriter, r *http.Request) {
		head := w.Header()
		head.Set("Content-Type", "application/json")
		w.WriteHeader(422)
		respondWith(w, `{"message":"Validation Failed", "errors": [{"resource":"Issue", "field": "title", "code": "missing_field"}]}`)
	})

	req, _ := client.NewRequest("error")
	_, err := req.Get(nil)
	assert.Contains(t, err.Error(), "422 - Validation Failed")
	assert.Contains(t, err.Error(), "missing_field error caused by title field on Issue resource")

	e := err.(*ResponseError)
	assert.Equal(t, ErrorUnprocessableEntity, e.Type)
}

func TestResponseError_Error_415(t *testing.T) {
	setup()
	defer tearDown()

	mux.HandleFunc("/error", func(w http.ResponseWriter, r *http.Request) {
		head := w.Header()
		head.Set("Content-Type", "application/json")
		w.WriteHeader(http.StatusUnsupportedMediaType)
		respondWith(w, `{"message":"Unsupported Media Type", "documentation_url":"http://developer.github.com/v3"}`)
	})

	req, _ := client.NewRequest("error")
	_, err := req.Get(nil)
	assert.Contains(t, err.Error(), "415 - Unsupported Media Type")
	assert.Contains(t, err.Error(), "// See: http://developer.github.com/v3")

	e := err.(*ResponseError)
	assert.Equal(t, ErrorUnsupportedMediaType, e.Type)
}

func TestResponseError_403(t *testing.T) {
	setup()
	defer tearDown()

	mux.HandleFunc("/error", func(w http.ResponseWriter, r *http.Request) {
		head := w.Header()
		head.Set("Content-Type", "application/json")
		w.WriteHeader(http.StatusForbidden)
		respondWith(w, `{"message":"Bad credentials", "documentation_url":"https://developer.github.com/v3/"}`)
	})

	req, _ := client.NewRequest("error")
	_, err := req.Get(nil)
	assert.Contains(t, err.Error(), "403 - Bad credentials")
	assert.Contains(t, err.Error(), "// See: https://developer.github.com/v3/")

	e := err.(*ResponseError)
	assert.Equal(t, ErrorForbidden, e.Type)
}
{'fraction_non_alphanumeric': 0.1800927878532265, 'fraction_numerical': 0.011071277941796711, 'mean_word_length': 5.13915857605178, 'pattern_counts': {'":': 26, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 3, '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': '6097472', 'n_tokens_mistral': 3492, 'n_tokens_neox': 3271, 'n_words': 634}