github.com/alibaba/ilogtail/pkg@v0.0.0-20250526110833-c53b480d046c/protocol/decoder/raw/decoder_test.go (about)

     1  // Copyright 2022 iLogtail Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package raw
    16  
    17  import (
    18  	"net/http"
    19  	"testing"
    20  
    21  	"github.com/stretchr/testify/assert"
    22  
    23  	"github.com/alibaba/ilogtail/pkg/models"
    24  )
    25  
    26  var data = `
    27  cpu.load.short,host=server01,region=cn value=0.64 
    28  `
    29  
    30  func TestNormal(t *testing.T) {
    31  	decoder := &Decoder{}
    32  	req := &http.Request{}
    33  	byteData := []byte(data)
    34  	groups, err := decoder.DecodeV2(byteData, req)
    35  	assert.Nil(t, err)
    36  	assert.Equal(t, len(groups), 1)
    37  	assert.Equal(t, len(groups[0].Events), 1)
    38  	event, ok := groups[0].Events[0].(models.ByteArray)
    39  	if !ok {
    40  		t.Errorf("raw decoder needs ByteArray")
    41  	}
    42  	assert.Equal(t, len(event), len(byteData))
    43  }
    44  
    45  func TestNormalV1(t *testing.T) {
    46  	decoder := &Decoder{}
    47  	req := &http.Request{}
    48  	byteData := []byte(data)
    49  	logs, err := decoder.Decode(byteData, req, make(map[string]string))
    50  	assert.Nil(t, err)
    51  	assert.Equal(t, len(logs), 1)
    52  	assert.Equal(t, len(logs[0].Contents), 1)
    53  	logContent := logs[0].Contents[0].Value
    54  	assert.Equal(t, len(logContent), len(byteData))
    55  }