<commit_before>// Copyright 2014 <NAME>.  All rights reserved.
// Use of this source code is governed by a MIT style
// license that can be found in the LICENSE file.

package gin

import (
	"encoding/base64"
	"net/http"
	"net/http/httptest"
	"testing"
)

func TestBasicAuthSucceed(t *testing.T) {
	req, _ := http.NewRequest("GET", "/login", nil)
	w := httptest.NewRecorder()

	r := New()
	accounts := Accounts{"admin": "password"}
	r.Use(BasicAuth(accounts))

	r.GET("/login", func(c *Context) {
		c.String(200, "autorized")
	})

	req.Header.Set("Authorization", "Basic "+base64.StdEncoding.EncodeToString([]byte("admin:password")))
	r.ServeHTTP(w, req)

	if w.Code != 200 {
		t.Errorf("Response code should be Ok, was: %s", w.Code)
	}
	bodyAsString := w.Body.String()

	if bodyAsString != "autorized" {
		t.Errorf("Response body should be `autorized`, was  %s", bodyAsString)
	}
}

func TestBasicAuth401(t *testing.T) {
	req, _ := http.NewRequest("GET", "/login", nil)
	w := httptest.NewRecorder()

	r := New()
	accounts := Accounts{"foo": "bar"}
	r.Use(BasicAuth(accounts))

	r.GET("/login", func(c *Context) {
		c.String(200, "autorized")
	})

	req.Header.Set("Authorization", "Basic "+base64.StdEncoding.EncodeToString([]byte("admin:password")))
	r.ServeHTTP(w, req)

	if w.Code != 401 {
		t.Errorf("Response code should be Not autorized, was: %s", w.Code)
	}

	if w.HeaderMap.Get("WWW-Authenticate") != "Basic realm=\"Authorization Required\"" {
		t.Errorf("WWW-Authenticate header is incorrect: %s", w.HeaderMap.Get("Content-Type"))
	}
}<commit_msg>Added test for custom realm<commit_after>// Copyright 2014 <NAME>.  All rights reserved.
// Use of this source code is governed by a MIT style
// license that can be found in the LICENSE file.

package gin

import (
	"encoding/base64"
	"net/http"
	"net/http/httptest"
	"testing"
)

func TestBasicAuthSucceed(t *testing.T) {
	req, _ := http.NewRequest("GET", "/login", nil)
	w := httptest.NewRecorder()

	r := New()
	accounts := Accounts{"admin": "password"}
	r.Use(BasicAuth(accounts))

	r.GET("/login", func(c *Context) {
		c.String(200, "autorized")
	})

	req.Header.Set("Authorization", "Basic "+base64.StdEncoding.EncodeToString([]byte("admin:password")))
	r.ServeHTTP(w, req)

	if w.Code != 200 {
		t.Errorf("Response code should be Ok, was: %s", w.Code)
	}
	bodyAsString := w.Body.String()

	if bodyAsString != "autorized" {
		t.Errorf("Response body should be `autorized`, was  %s", bodyAsString)
	}
}

func TestBasicAuth401(t *testing.T) {
	req, _ := http.NewRequest("GET", "/login", nil)
	w := httptest.NewRecorder()

	r := New()
	accounts := Accounts{"foo": "bar"}
	r.Use(BasicAuth(accounts))

	r.GET("/login", func(c *Context) {
		c.String(200, "autorized")
	})

	req.Header.Set("Authorization", "Basic "+base64.StdEncoding.EncodeToString([]byte("admin:password")))
	r.ServeHTTP(w, req)

	if w.Code != 401 {
		t.Errorf("Response code should be Not autorized, was: %s", w.Code)
	}

	if w.HeaderMap.Get("WWW-Authenticate") != "Basic realm=\"Authorization Required\"" {
		t.Errorf("WWW-Authenticate header is incorrect: %s", w.HeaderMap.Get("Content-Type"))
	}
}

func TestBasicAuth401WithCustomRealm(t *testing.T) {
	req, _ := http.NewRequest("GET", "/login", nil)
	w := httptest.NewRecorder()

	r := New()
	accounts := Accounts{"foo": "bar"}
	r.Use(BasicAuthForRealm(accounts, "My Custom Realm"))

	r.GET("/login", func(c *Context) {
		c.String(200, "autorized")
	})

	req.Header.Set("Authorization", "Basic "+base64.StdEncoding.EncodeToString([]byte("admin:password")))
	r.ServeHTTP(w, req)

	if w.Code != 401 {
		t.Errorf("Response code should be Not autorized, was: %s", w.Code)
	}

	if w.HeaderMap.Get("WWW-Authenticate") != "Basic realm=\"My Custom Realm\"" {
		t.Errorf("WWW-Authenticate header is incorrect: %s", w.HeaderMap.Get("Content-Type"))
	}
}
{'fraction_non_alphanumeric': 0.18441014909756737, 'fraction_numerical': 0.01595605545383207, 'mean_word_length': 4.566229985443959, 'pattern_counts': {'":': 5, '<': 5, '<?xml version=': 0, '>': 5, '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': '4252240', 'n_tokens_mistral': 1478, 'n_tokens_neox': 1404, 'n_words': 318}