<commit_before>package moon

import (
	"fmt"
	"io/ioutil"
	"log"
	"net/http"
	"net/http/httptest"
	"strings"
	"testing"

	"github.com/goji/httpauth"
	"golang.org/x/net/context"
)

func assertEquals(t *testing.T, e interface{}, o interface{}) {
	if e != o {
		t.Errorf("\n...expected = %v\n...obtained = %v", e, o)
	}
}

func assertHasPrefix(t *testing.T, o interface{}, prefix string) {
	if strings.HasPrefix(o.(string), prefix) == false {
		t.Errorf("\n...expected prefix = %v\n...obtained = %v", prefix, o)
	}
}

func serveAndRequest(h http.Handler, auth bool) string {
	//auth := base64.StdEncoding.EncodeToString([]byte("user:pass"))
	//r.Header.Set("Authorization", "Basic "+auth)

	ts := httptest.NewServer(h)
	defer ts.Close()

	req, _ := http.NewRequest("GET", ts.URL, nil)

	if auth {
		req.SetBasicAuth("user", "pass")
	}

	res, err := http.DefaultClient.Do(req)
	//res, err := http.Client.Do(req)

	// res, err := http.Get(ts.URL)
	if err != nil {
		log.Fatal(err)
	}
	resBody, err := ioutil.ReadAll(res.Body)
	res.Body.Close()
	if err != nil {
		log.Fatal(err)
	}
	//	log.Print(resBody)
	return string(resBody)
}

func tokenMiddlewareA(ctx context.Context, next Next) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		ctx = context.WithValue(ctx, "tokenA", "123")
		next(ctx)
	})
}

func tokenMiddlewareB(ctx context.Context, next Next) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		ctx = context.WithValue(ctx, "tokenB", "<PASSWORD>")
		next(ctx)
	})
}

func tokenHandler(ctx context.Context) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		fmt.Fprintf(w, "Tokens are: %v, %v", ctx.Value("tokenA"), ctx.Value("tokenB"))
	})
}

func TestContext(t *testing.T) {
	st := New(tokenMiddlewareA, tokenMiddlewareB).Then(tokenHandler)
	res := serveAndRequest(st, false)
	assertEquals(t, "Tokens are: 123, 456", res)
}

func TestContextRoot(t *testing.T) {
	Context = func(r *http.Request) context.Context {
		ctx := context.TODO()
		return context.WithValue(ctx, "tokenA", "789")
	}
	defer func() { Context = nil }()
	st := New(tokenMiddlewareB).Then(tokenHandler)
	res := serveAndRequest(st, false)
	assertEquals(t, "Tokens are: 789, 456", res)
}

func authHandler(ctx context.Context) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		u, p, _ := r.BasicAuth()
		fmt.Fprintf(w, "Hello: %v, %v", u, p)
	})
}

func TestGojiBasicAuthUnauthorized(t *testing.T) {
	st := New(Adapt(httpauth.SimpleBasicAuth("user", "pass"))).Then(authHandler)
	res := serveAndRequest(st, false)
	assertEquals(t, "Unauthorized\n", res)
}

func TestGojiBasicAuthAuthorized(t *testing.T) {
	st := New(Adapt(httpauth.SimpleBasicAuth("user", "pass"))).Then(authHandler)
	res := serveAndRequest(st, true)
	assertEquals(t, "Hello: user, pass", res)
}

func TestContextWithFunc(t *testing.T) {
	st := New(tokenMiddlewareA, tokenMiddlewareB).ThenFunc(func(ctx context.Context, w http.ResponseWriter, r *http.Request) {
		fmt.Fprintf(w, "Tokens are: %v, %v", ctx.Value("tokenA"), ctx.Value("tokenB"))
	})
	res := serveAndRequest(st, false)
	assertEquals(t, "Tokens are: 123, 456", res)
}<commit_msg>Rename test vars<commit_after>package moon

import (
	"fmt"
	"io/ioutil"
	"log"
	"net/http"
	"net/http/httptest"
	"strings"
	"testing"

	"github.com/goji/httpauth"
	"golang.org/x/net/context"
)

func assertEquals(t *testing.T, e interface{}, o interface{}) {
	if e != o {
		t.Errorf("\n...expected = %v\n...obtained = %v", e, o)
	}
}

func assertHasPrefix(t *testing.T, o interface{}, prefix string) {
	if strings.HasPrefix(o.(string), prefix) == false {
		t.Errorf("\n...expected prefix = %v\n...obtained = %v", prefix, o)
	}
}

func serveAndRequest(h http.Handler, auth bool) string {
	//auth := base64.StdEncoding.EncodeToString([]byte("user:pass"))
	//r.Header.Set("Authorization", "Basic "+auth)

	ts := httptest.NewServer(h)
	defer ts.Close()

	req, _ := http.NewRequest("GET", ts.URL, nil)

	if auth {
		req.SetBasicAuth("user", "pass")
	}

	res, err := http.DefaultClient.Do(req)
	//res, err := http.Client.Do(req)

	// res, err := http.Get(ts.URL)
	if err != nil {
		log.Fatal(err)
	}
	resBody, err := ioutil.ReadAll(res.Body)
	res.Body.Close()
	if err != nil {
		log.Fatal(err)
	}
	//	log.Print(resBody)
	return string(resBody)
}

func tokenMiddlewareA(ctx context.Context, next Next) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		ctx = context.WithValue(ctx, "tokenA", "123")
		next(ctx)
	})
}

func tokenMiddlewareB(ctx context.Context, next Next) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		ctx = context.WithValue(ctx, "tokenB", "456")
		next(ctx)
	})
}

func tokenHandler(ctx context.Context) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		fmt.Fprintf(w, "Tokens are: %v, %v", ctx.Value("tokenA"), ctx.Value("tokenB"))
	})
}

func TestContext(t *testing.T) {
	st := New(tokenMiddlewareA, tokenMiddlewareB).Then(tokenHandler)
	res := serveAndRequest(st, false)
	assertEquals(t, "Tokens are: 123, 456", res)
}

func TestContextRoot(t *testing.T) {
	Context = func(r *http.Request) context.Context {
		ctx := context.TODO()
		return context.WithValue(ctx, "tokenA", "789")
	}
	defer func() { Context = nil }()
	mws := New(tokenMiddlewareB).Then(tokenHandler)
	res := serveAndRequest(mws, false)
	assertEquals(t, "Tokens are: 789, 456", res)
}

func authHandler(ctx context.Context) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		u, p, _ := r.BasicAuth()
		fmt.Fprintf(w, "Hello: %v, %v", u, p)
	})
}

func TestGojiBasicAuthUnauthorized(t *testing.T) {
	mws := New(Adapt(httpauth.SimpleBasicAuth("user", "pass"))).Then(authHandler)
	res := serveAndRequest(mws, false)
	assertEquals(t, "Unauthorized\n", res)
}

func TestGojiBasicAuthAuthorized(t *testing.T) {
	mws := New(Adapt(httpauth.SimpleBasicAuth("user", "pass"))).Then(authHandler)
	res := serveAndRequest(mws, true)
	assertEquals(t, "Hello: user, pass", res)
}

func TestContextWithFunc(t *testing.T) {
	mws := New(tokenMiddlewareA, tokenMiddlewareB).ThenFunc(func(ctx context.Context, w http.ResponseWriter, r *http.Request) {
		fmt.Fprintf(w, "Tokens are: %v, %v", ctx.Value("tokenA"), ctx.Value("tokenB"))
	})
	res := serveAndRequest(mws, false)
	assertEquals(t, "Tokens are: 123, 456", res)
}
{'fraction_non_alphanumeric': 0.17805255023183925, 'fraction_numerical': 0.008500772797527048, 'mean_word_length': 4.762243989314337, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, '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': '1244159', 'n_tokens_mistral': 2497, 'n_tokens_neox': 2424, 'n_words': 508}