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  // +build !nethttpomithttp2
     7  
     8  package http
     9  
    10  import (
    11  	"errors"
    12  	"fmt"
    13  	"testing"
    14  )
    15  
    16  type externalStreamErrorCode uint32
    17  
    18  type externalStreamError struct {
    19  	StreamID uint32
    20  	Code     externalStreamErrorCode
    21  	Cause    error
    22  }
    23  
    24  func (e externalStreamError) Error() string {
    25  	return fmt.Sprintf("ID %v, code %v", e.StreamID, e.Code)
    26  }
    27  
    28  func TestStreamError(t *testing.T) {
    29  	var target externalStreamError
    30  	streamErr := http2streamError(42, http2ErrCodeProtocol)
    31  	ok := errors.As(streamErr, &target)
    32  	if !ok {
    33  		t.Fatalf("errors.As failed")
    34  	}
    35  	if target.StreamID != streamErr.StreamID {
    36  		t.Errorf("got StreamID %v, expected %v", target.StreamID, streamErr.StreamID)
    37  	}
    38  	if target.Cause != streamErr.Cause {
    39  		t.Errorf("got Cause %v, expected %v", target.Cause, streamErr.Cause)
    40  	}
    41  	if uint32(target.Code) != uint32(streamErr.Code) {
    42  		t.Errorf("got Code %v, expected %v", target.Code, streamErr.Code)
    43  	}
    44  }
    45  

View as plain text