<commit_before>package rhttp

import (
	"fmt"
	"reflect"
	"regexp"
	"strings"
	"testing"

	"net/http"
)

type ResponseWriterMock int

func (*ResponseWriterMock) Header() http.Header         { return make(http.Header) }
func (*ResponseWriterMock) Write(b []byte) (int, error) { return len(b), nil }
func (r *ResponseWriterMock) WriteHeader(code int)      { *r = ResponseWriterMock(code) }
func (r *ResponseWriterMock) GetCode() int              { return int(*r) }

func ExampleRegexpRouter() {
	w := new(ResponseWriterMock)
	type P struct {
		S string `rhttp:"s"`
	}

	f := func(w http.ResponseWriter, r *http.Request, i interface{}) {
		params := i.(*P)
		fmt.Printf("%s\n", params.S)
	}

	router := NewRouter()
	router.HandleFunc("/(s=[abc])", f, &P{})

	r, _ := http.NewRequest("GET", "/a", strings.NewReader(""))
	router.ServeHTTP(w, r)

	r, _ = http.NewRequest("GET", "/b", strings.NewReader(""))
	router.ServeHTTP(w, r)

	w.WriteHeader(0)
	r, _ = http.NewRequest("GET", "/d", strings.NewReader(""))
	router.ServeHTTP(w, r)
	fmt.Printf("%d\n", w.GetCode())

	// Output:
	// a
	// b
	// 404
}

func ExampleRegexpRouterMulti() {
	w := new(ResponseWriterMock)
	type P struct {
		S string `rhttp:"s"`
	}

	f := func(name string) func(w http.ResponseWriter, r *http.Request, i interface{}) {
		return func(w http.ResponseWriter, r *http.Request, i interface{}) {
			params := i.(*P)
			fmt.Printf("%s: %s\n", name, params.S)
		}
	}

	router := NewRouter()
	router.HandleFunc("/a(s=[12])(/?)", f("a1"), &P{})
	router.HandleFunc("/a(s=[123])", f("a2"), &P{})
	router.HandleFunc("/b(s=[123])", f("b"), &P{})

	r, _ := http.NewRequest("GET", "/a1", strings.NewReader(""))
	router.ServeHTTP(w, r)<commit_msg>Added user defined type example.<commit_after>package rhttp

import (
	"fmt"
	"reflect"
	"regexp"
	"strings"
	"testing"

	"net/http"
)

type ResponseWriterMock int

func (*ResponseWriterMock) Header() http.Header         { return make(http.Header) }
func (*ResponseWriterMock) Write(b []byte) (int, error) { return len(b), nil }
func (r *ResponseWriterMock) WriteHeader(code int)      { *r = ResponseWriterMock(code) }
func (r *ResponseWriterMock) GetCode() int              { return int(*r) }

func ExampleRegexpRouter() {
	w := new(ResponseWriterMock)
	type P struct {
		S string `rhttp:"s"`
	}

	f := func(w http.ResponseWriter, r *http.Request, i interface{}) {
		params := i.(*P)
		fmt.Printf("%s\n", params.S)
	}

	router := NewRouter()
	router.HandleFunc("/(s=[abc])", f, &P{})

	r, _ := http.NewRequest("GET", "/a", strings.NewReader(""))
	router.ServeHTTP(w, r)

	r, _ = http.NewRequest("GET", "/b", strings.NewReader(""))
	router.ServeHTTP(w, r)

	w.WriteHeader(0)
	r, _ = http.NewRequest("GET", "/d", strings.NewReader(""))
	router.ServeHTTP(w, r)
	fmt.Printf("%d\n", w.GetCode())

	// Output:
	// a
	// b
	// 404
}

func ExampleRegexpRouterWithUserType() {
	w := new(ResponseWriterMock)
	type UserId int64
	type P struct {
		Id UserId `rhttp:"userId"`
	}

	f := func(w http.ResponseWriter, r *http.Request, i interface{}) {
		params := i.(*P)
		fmt.Printf("%v\n", params.Id)
	}

	router := NewRouter()
	router.HandleFunc("/(userId=.*)", f, &P{})

	r, _ := http.NewRequest("GET", "/1234", strings.NewReader(""))
	router.ServeHTTP(w, r)

	// Output:
	// 1234
}

func ExampleRegexpRouterMulti() {
	w := new(ResponseWriterMock)
	type P struct {
		S string `rhttp:"s"`
	}

	f := func(name string) func(w http.ResponseWriter, r *http.Request, i interface{}) {
		return func(w http.ResponseWriter, r *http.Request, i interface{}) {
			params := i.(*P)
			fmt.Printf("%s: %s\n", name, params.S)
		}
	}

	router := NewRouter()
	router.HandleFunc("/a(s=[12])(/?)", f("a1"), &P{})
	router.HandleFunc("/a(s=[123])", f("a2"), &P{})
	router.HandleFunc("/b(s=[123])", f("b"), &P{})

	r, _ := http.NewRequest("GET", "/a1", strings.NewReader(""))
	router.ServeHTTP(w, r)
{'fraction_non_alphanumeric': 0.2112748900956814, 'fraction_numerical': 0.01034393586759762, 'mean_word_length': 4.069462647444299, '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': '6762391', 'n_tokens_mistral': 1523, 'n_tokens_neox': 1461, 'n_words': 322}