decred.org/dcrdex@v1.0.5/dex/dexnet/http_test.go (about)

     1  package dexnet
     2  
     3  import (
     4  	"context"
     5  	"net/http"
     6  	"net/http/httptest"
     7  	"testing"
     8  )
     9  
    10  func TestErrorParsing(t *testing.T) {
    11  	ctx, cancel := context.WithCancel(context.Background())
    12  	defer cancel()
    13  
    14  	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    15  		http.Error(w, `{"code": -150, "msg": "you messed up, bruh"}`, http.StatusBadRequest)
    16  	}))
    17  	defer ts.Close()
    18  
    19  	var errPayload struct {
    20  		Code int    `json:"code"`
    21  		Msg  string `json:"msg"`
    22  	}
    23  	if err := Get(ctx, ts.URL, nil, WithErrorParsing(&errPayload)); err == nil {
    24  		t.Fatal("didn't get an http error")
    25  	}
    26  	if errPayload.Code != -150 || errPayload.Msg != "you messed up, bruh" {
    27  		t.Fatal("unexpected error body")
    28  	}
    29  }