github.com/Bytom/bytom@v1.1.2-0.20210127130405-ae40204c0b09/net/http/httpjson/context_test.go (about)

     1  package httpjson
     2  
     3  import (
     4  	"context"
     5  	"net/http"
     6  	"net/http/httptest"
     7  	"testing"
     8  )
     9  
    10  func TestContext(t *testing.T) {
    11  	wantHead := "bar"
    12  	wantRespHead := "baz"
    13  	f := func(ctx context.Context) {
    14  		if g := Request(ctx).Header.Get("Test-Key"); g != wantHead {
    15  			t.Errorf("header = %q want %q", g, wantHead)
    16  		}
    17  		ResponseWriter(ctx).Header().Set("Test-Resp-Key", wantRespHead)
    18  	}
    19  
    20  	h, err := Handler(f, nil)
    21  	if err != nil {
    22  		t.Fatalf("err = %v", err)
    23  	}
    24  
    25  	resp := httptest.NewRecorder()
    26  	req, _ := http.NewRequest("GET", "/", nil)
    27  	req.Header.Set("Test-Key", wantHead)
    28  	h.ServeHTTP(resp, req)
    29  	if g := resp.Header().Get("Test-Resp-Key"); g != wantRespHead {
    30  		t.Errorf("header = %q want %q", g, wantRespHead)
    31  	}
    32  }