Source file src/net/http/h2_error_test.go

     1  // Copyright 2022 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  //go:build !nethttpomithttp2
     6  
     7  package http
     8  
     9  import (
    10  	"errors"
    11  	"fmt"
    12  	"testing"
    13  )
    14  
    15  type externalStreamErrorCode uint32
    16  
    17  type externalStreamError struct {
    18  	StreamID uint32
    19  	Code     externalStreamErrorCode
    20  	Cause    error
    21  }
    22  
    23  func (e externalStreamError) Error() string {
    24  	return fmt.Sprintf("ID %v, code %v", e.StreamID, e.Code)
    25  }
    26  
    27  func TestStreamError(t *testing.T) {
    28  	streamErr := http2streamError(42, http2ErrCodeProtocol)
    29  	extStreamErr, ok := errors.AsType[externalStreamError](streamErr)
    30  	if !ok {
    31  		t.Fatalf("errors.AsType failed")
    32  	}
    33  	if extStreamErr.StreamID != streamErr.StreamID {
    34  		t.Errorf("got StreamID %v, expected %v", extStreamErr.StreamID, streamErr.StreamID)
    35  	}
    36  	if extStreamErr.Cause != streamErr.Cause {
    37  		t.Errorf("got Cause %v, expected %v", extStreamErr.Cause, streamErr.Cause)
    38  	}
    39  	if uint32(extStreamErr.Code) != uint32(streamErr.Code) {
    40  		t.Errorf("got Code %v, expected %v", extStreamErr.Code, streamErr.Code)
    41  	}
    42  }
    43  

View as plain text