func TestRealNetworking(t *testing.T) {
	srv := &http.Server{Addr: "localhost:9876"}
	finish := make(chan struct{})
	tokenValue := "ABCDEF"
	http.HandleFunc("/login", func(w http.ResponseWriter, r *http.Request) {
		http.SetCookie(w, &http.Cookie{Name: "Token", Value: tokenValue})
		w.WriteHeader(203)
	})
	http.HandleFunc("/authenticated_resource", func(w http.ResponseWriter, r *http.Request) {
		token, err := r.Cookie("Token")
		if err == http.ErrNoCookie {
			w.WriteHeader(400)
			return
		}
		if err != nil {
			w.WriteHeader(500)
			return
		}

		if token.Value != tokenValue {
			t.Fatalf("token did not equal %s", tokenValue)
		}
		w.WriteHeader(204)
	})

	go func() {
		if err := srv.ListenAndServe(); err != nil {
			panic(err)
		}
	}()

	go func() {
		defer func() {
			if r := recover(); r != nil {
				fmt.Println("Recovered in f", r)
			}
		}()

		cookieJar, _ := cookiejar.New(nil)
		cli := &http.Client{
			Timeout: time.Second * 1,
			Jar:     cookieJar,
		}

		apitest.New().
			EnableNetworking(cli).
			Get("http://localhost:9876/login").
			Expect(t).
			Status(203).
			End()

		apitest.New().
			EnableNetworking(cli).
			Get("http://localhost:9876/authenticated_resource").
			Expect(t).
			Status(204).
			End()

		finish <- struct{}{}
	}()
	<-finish
}
// TestRealNetworking creates a server with two endpoints, /login sets a token via a cookie and /authenticated_resource
// validates the token. A cookie jar is used to verify session persistence across multiple apitest instances
TestRealNetworking creates a server with two endpoints, /login sets a token via a cookie and /authenticated_resource
validates the token. A cookie jar is used to verify session persistence across multiple apitest instances
// TestRealNetworking creates a server with two endpoints / login sets a token via a cookie and / authenticated_resource // validates the token . A cookie jar is used to verify session persistence across multiple apitest instances
func TestRealNetworking(t *testing.T) {
	srv := &http.Server{Addr: "localhost:9876"}
	finish := make(chan struct{})
	tokenValue := "ABCDEF"
	http.HandleFunc("/login", func(w http.ResponseWriter, r *http.Request) {
		http.SetCookie(w, &http.Cookie{Name: "Token", Value: tokenValue})
		w.WriteHeader(203)
	})
	http.HandleFunc("/authenticated_resource", func(w http.ResponseWriter, r *http.Request) {
		token, err := r.Cookie("Token")
		if err == http.ErrNoCookie {
			w.WriteHeader(400)
			return
		}
		if err != nil {
			w.WriteHeader(500)
			return
		}
		if token.Value != tokenValue {
			t.Fatalf("token did not equal %s", tokenValue)
		}
		w.WriteHeader(204)
	})
	go func() {
		if err := srv.ListenAndServe(); err != nil {
			panic(err)
		}
	}()
	go func() {
		defer func() {
			if r := recover(); r != nil {
				fmt.Println("Recovered in f", r)
			}
		}()
		cookieJar, _ := cookiejar.New(nil)
		cli := &http.Client{
			Timeout: time.Second * 1,
			Jar:     cookieJar,
		}
		apitest.New().
			EnableNetworking(cli).
			Get("http://localhost:9876/login").
			Expect(t).
			Status(203).
			End()
		apitest.New().
			EnableNetworking(cli).
			Get("http://localhost:9876/authenticated_resource").
			Expect(t).
			Status(204).
			End()
		finish <- struct{}{}
	}()
	<-finish
}
func TestRealNetworking ( t * testing . T ) { srv := & http . Server { Addr : " " } finish := make ( chan struct { } ) tokenValue := " " http . HandleFunc ( " " , func ( w http . ResponseWriter , r * http . Request ) { http . SetCookie ( w , & http . Cookie { Name : " " , Value : tokenValue } ) w . WriteHeader ( 203 ) } ) http . HandleFunc ( " " , func ( w http . ResponseWriter , r * http . Request ) { token , err := r . Cookie ( " " ) if err == http . ErrNoCookie { w . WriteHeader ( 400 ) return } if err != nil { w . WriteHeader ( 500 ) return } if token . Value != tokenValue { t . Fatalf ( " " , tokenValue ) } w . WriteHeader ( 204 ) } ) go func ( ) { if err := srv . ListenAndServe ( ) ; err != nil { panic ( err ) } } ( ) go func ( ) { defer func ( ) { if r := recover ( ) ; r != nil { fmt . Println ( " " , r ) } } ( ) cookieJar , _ := cookiejar . New ( nil ) cli := & http . Client { Timeout : time . Second * 1 , Jar : cookieJar , } apitest . New ( ) . EnableNetworking ( cli ) . Get ( " " ) . Expect ( t ) . Status ( 203 ) . End ( ) apitest . New ( ) . EnableNetworking ( cli ) . Get ( " " ) . Expect ( t ) . Status ( 204 ) . End ( ) finish <- struct { } { } } ( ) <- finish }
TestRealNetworking creates a server with two endpoints, /login sets a token via a cookie and /authenticated_resource
validates the token.
TestRealNetworking creates a server with two endpoints / login sets a token via a cookie and / authenticated_resource validates the token .