<commit_before>package xhttp_test

import (
	"fmt"
	"log"
	"net/http"
	"net/http/httptest"

	"github.com/atdiar/goroutine/execution"
	"github.com/atdiar/xhttp"
)

// Let's create a catchAll Handler i.e. an object implementing HandlerLinker.
// It's also what people may call a catchall middleware.
// This should illustrate one of the form a HandlerLinker can take.
type middlewareExample struct {
	next xhttp.Handler
}

func (m middlewareExample) ServeHTTP(ctx execution.Context, w http.ResponseWriter, r *http.Request) {
	fmt.Fprint(w, "OK ")
	if m.next != nil {
		m.next.ServeHTTP(ctx, w, r)
	}
}

func (m middlewareExample) CallNext(h xhttp.Handler) xhttp.HandlerLinker {
	m.next = h
	return m
}

// The Example starts here.
func Example() {
	s := xhttp.NewServeMux()

	s.USE(middlewareExample{})

	s.GET("/", xhttp.HandlerFunc(func(ctx execution.Context, res http.ResponseWriter, req *http.Request) {

		fmt.Fprint(res, "OK")
	}))

	req, err := http.NewRequest("GET", "http://example.com/", nil)
	if err != nil {
		log.Fatal(err)
	}

	w := httptest.NewRecorder()
	s.ServeHTTP(w, req)

	fmt.Printf("%d - %s", w.Code, w.Body.String())
	// Output: 200 - OK OK
}
<commit_msg>Modify Example to test out whether context storage works.<commit_after>package xhttp_test

import (
	"fmt"
	"log"
	"net/http"
	"net/http/httptest"

	"github.com/atdiar/goroutine/execution"
	"github.com/atdiar/xhttp"
)

// Let's create a catchAll Handler i.e. an object implementing HandlerLinker.
// It's also what people may call a catchall middleware.
// This should illustrate one of the form a HandlerLinker can take.
type middlewareExample struct {
	next xhttp.Handler
}

func (m middlewareExample) ServeHTTP(ctx execution.Context, w http.ResponseWriter, r *http.Request) {
	fmt.Fprint(w, "OK ")
	if m.next != nil {
		m.next.ServeHTTP(ctx, w, r)
	}
}

func (m middlewareExample) CallNext(h xhttp.Handler) xhttp.HandlerLinker {
	m.next = h
	return m
}

// The Example starts here.
func Example() {
	s := xhttp.NewServeMux()

	s.USE(middlewareExample{})

	s.GET("/", xhttp.HandlerFunc(func(ctx execution.Context, res http.ResponseWriter, req *http.Request) {
		ctx.Put("test", "OK")
		val, err := ctx.Get("test")
		if err != nil {
			panic(err) // for the sake of the example, we will panic. Not idiomatic.
		}
		fmt.Fprint(res, val)
	}))

	req, err := http.NewRequest("GET", "http://example.com/", nil)
	if err != nil {
		log.Fatal(err)
	}

	w := httptest.NewRecorder()
	s.ServeHTTP(w, req)

	fmt.Printf("%d - %s", w.Code, w.Body.String())
	// Output: 200 - OK OK
}

{'fraction_non_alphanumeric': 0.14925373134328357, 'fraction_numerical': 0.002356637863315004, 'mean_word_length': 3.778611632270169, 'pattern_counts': {'":': 0, '<': 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': '3897628', 'n_tokens_mistral': 988, 'n_tokens_neox': 941, 'n_words': 248}