github.com/puellanivis/breton@v0.2.16/lib/net/hls/m3u8/attributelist_test.go (about)

     1  package m3u8
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  	"time"
     7  )
     8  
     9  func TestMarshalKey(t *testing.T) {
    10  	key := &Key{
    11  		Method: "NONE",
    12  		URI:    "scheme://host:port/path?query#fragment",
    13  
    14  		KeyFormatVersions: []int{2, 3, 5, 7},
    15  	}
    16  
    17  	s, err := marshalAttributeList(key)
    18  	if err != nil {
    19  		t.Fatalf("%+v", err)
    20  	}
    21  
    22  	expected := `METHOD=NONE,URI="scheme://host:port/path?query#fragment",KEYFORMATVERSIONS="2/3/5/7"`
    23  	if s != expected {
    24  		t.Errorf("expected; but got:\n\t%s\n\t%s", expected, s)
    25  	}
    26  
    27  	test := new(Key)
    28  
    29  	if err := unmarshalAttributeList(test, []byte(expected)); err != nil {
    30  		t.Fatalf("%+v", err)
    31  	}
    32  
    33  	if !reflect.DeepEqual(test, key) {
    34  		t.Errorf("unmarshal failed, expected; but got:\n\t%#v\n\t%#v", key, test)
    35  	}
    36  }
    37  
    38  func TestMarshalStreamInf(t *testing.T) {
    39  	sinf := &StreamInf{
    40  		Bandwidth:  1234567,
    41  		Codecs:     []string{"codec1", `"quoted text"`},
    42  		Resolution: Resolution{Width: 1024, Height: 768},
    43  		FrameRate:  50,
    44  		Audio:      "audio-group-id",
    45  	}
    46  
    47  	s, err := marshalAttributeList(sinf)
    48  	if err != nil {
    49  		t.Fatalf("%+v", err)
    50  	}
    51  
    52  	expected := `BANDWIDTH=1234567,CODECS="codec1,\"quoted text\"",RESOLUTION=1024x768,FRAME-RATE=50,AUDIO="audio-group-id"`
    53  	if s != expected {
    54  		t.Errorf("expected; but got:\n\t%s\n\t%s", expected, s)
    55  	}
    56  
    57  	test := new(StreamInf)
    58  
    59  	if err := unmarshalAttributeList(test, []byte(expected)); err != nil {
    60  		t.Fatalf("%+v", err)
    61  	}
    62  
    63  	if !reflect.DeepEqual(test, sinf) {
    64  		t.Errorf("unmarshal failed, expected; but got:\n\t%#v\n\t%#v", sinf, test)
    65  	}
    66  
    67  	sinf.FrameRate = 59.997
    68  	s, err = marshalAttributeList(sinf)
    69  	if err != nil {
    70  		t.Fatalf("%+v", err)
    71  	}
    72  
    73  	expected = `BANDWIDTH=1234567,CODECS="codec1,\"quoted text\"",RESOLUTION=1024x768,FRAME-RATE=59.997,AUDIO="audio-group-id"`
    74  	if s != expected {
    75  		t.Errorf("expected; but got:\n\t%s\n\t%s", expected, s)
    76  	}
    77  
    78  	test = new(StreamInf)
    79  
    80  	if err := unmarshalAttributeList(test, []byte(expected)); err != nil {
    81  		t.Fatalf("%+v", err)
    82  	}
    83  
    84  	if !reflect.DeepEqual(test, sinf) {
    85  		t.Errorf("unmarshal failed, expected; but got:\n\t%#v\n\t%#v", sinf, test)
    86  	}
    87  }
    88  
    89  func TestMarshalDateRange(t *testing.T) {
    90  	dr := &DateRange{
    91  		ID:        "identification",
    92  		StartDate: time.Date(2018, 03, 07, 15, 52, 36, 0, time.UTC),
    93  		Duration:  5 * time.Second,
    94  		ClientAttribute: map[string]interface{}{
    95  			"STR": `"XYZ123"`,
    96  			"INT": 42,
    97  			"HEX": []byte{2, 3, 5, 7},
    98  		},
    99  	}
   100  
   101  	s, err := marshalAttributeList(dr)
   102  	if err != nil {
   103  		t.Fatalf("%+v", err)
   104  	}
   105  
   106  	expected := `ID="identification",START-DATE=2018-03-07T15:52:36Z,DURATION=5,X-HEX=0x02030507,X-INT=42,X-STR="\"XYZ123\""`
   107  	if s != expected {
   108  		t.Errorf("expected; but got:\n\t%s\n\t%s", expected, s)
   109  	}
   110  
   111  	test := new(DateRange)
   112  
   113  	if err := unmarshalAttributeList(test, []byte(expected)); err != nil {
   114  		t.Fatalf("%+v", err)
   115  	}
   116  
   117  	test.ClientAttribute = nil
   118  	testDR := *dr
   119  	testDR.ClientAttribute = nil
   120  	if !reflect.DeepEqual(test, &testDR) {
   121  		t.Errorf("unmarshal failed, expected; but got:\n\t%#v\n\t%#v", &testDR, test)
   122  	}
   123  
   124  	dr.EndOnNext = true
   125  	s, err = marshalAttributeList(dr)
   126  	if err != nil {
   127  		t.Fatalf("%+v", err)
   128  	}
   129  
   130  	expected = `ID="identification",START-DATE=2018-03-07T15:52:36Z,DURATION=5,END-ON-NEXT=YES,X-HEX=0x02030507,X-INT=42,X-STR="\"XYZ123\""`
   131  	if s != expected {
   132  		t.Errorf("expected; but got:\n\t%s\n\t%s", expected, s)
   133  	}
   134  
   135  	test = new(DateRange)
   136  
   137  	if err := unmarshalAttributeList(test, []byte(expected)); err != nil {
   138  		t.Fatalf("%+v", err)
   139  	}
   140  
   141  	test.ClientAttribute = nil
   142  	testDR = *dr
   143  	testDR.ClientAttribute = nil
   144  	if !reflect.DeepEqual(test, &testDR) {
   145  		t.Errorf("unmarshal failed, expected; but got:\n\t%#v\n\t%#v", &testDR, test)
   146  	}
   147  }
   148  
   149  func TestMarshalMedia(t *testing.T) {
   150  	m := &Media{
   151  		Type:       "AUDIO",
   152  		GroupID:    "audio-mp4a.40.2",
   153  		Name:       "Français",
   154  		Default:    true,
   155  		Autoselect: true,
   156  		Language:   "fr",
   157  		URI:        "path?query#fragment",
   158  	}
   159  
   160  	s, err := marshalAttributeList(m)
   161  	if err != nil {
   162  		t.Fatalf("%+v", err)
   163  	}
   164  
   165  	expected := `TYPE=AUDIO,GROUP-ID="audio-mp4a.40.2",NAME="Français",DEFAULT=YES,AUTOSELECT=YES,LANGUAGE="fr",URI="path?query#fragment"`
   166  	if s != expected {
   167  		t.Errorf("expected; but got:\n\t%s\n\t%s", expected, s)
   168  	}
   169  
   170  	test := new(Media)
   171  
   172  	if err := unmarshalAttributeList(test, []byte(expected)); err != nil {
   173  		t.Fatalf("%+v", err)
   174  	}
   175  
   176  	if !reflect.DeepEqual(test, m) {
   177  		t.Errorf("unmarshal failed, expected; but got:\n\t%#v\n\t%#v", m, test)
   178  	}
   179  
   180  	m.Channels = []int{7, 2}
   181  
   182  	s, err = marshalAttributeList(m)
   183  	if err != nil {
   184  		t.Fatalf("%+v", err)
   185  	}
   186  
   187  	expected = `TYPE=AUDIO,GROUP-ID="audio-mp4a.40.2",NAME="Français",DEFAULT=YES,AUTOSELECT=YES,LANGUAGE="fr",CHANNELS="7/2",URI="path?query#fragment"`
   188  	if s != expected {
   189  		t.Errorf("expected; but got:\n\t%s\n\t%s", expected, s)
   190  	}
   191  
   192  	test = new(Media)
   193  
   194  	if err := unmarshalAttributeList(test, []byte(expected)); err != nil {
   195  		t.Fatalf("%+v", err)
   196  	}
   197  
   198  	if !reflect.DeepEqual(test, m) {
   199  		t.Errorf("unmarshal failed, expected; but got:\n\t%#v\n\t%#v", m, test)
   200  	}
   201  }