github.com/optim-corp/cios-golang-sdk@v0.5.1/sdk/service_video_streaming_test.go (about)

     1  package ciossdk
     2  
     3  import (
     4  	"encoding/json"
     5  	"net/http"
     6  	"net/http/httptest"
     7  	"net/url"
     8  	"testing"
     9  
    10  	srvvideo "github.com/optim-corp/cios-golang-sdk/sdk/service/video"
    11  
    12  	cnv "github.com/fcfcqloow/go-advance/convert"
    13  
    14  	"github.com/optim-corp/cios-golang-sdk/cios"
    15  
    16  	sdkmodel "github.com/optim-corp/cios-golang-sdk/model"
    17  )
    18  
    19  func TestVideo_Videos(t *testing.T) {
    20  	var (
    21  		query url.Values
    22  		tests = []struct {
    23  			params cios.ApiGetVideoStreamsListRequest
    24  			test   func()
    25  		}{
    26  			{
    27  				params: srvvideo.MakeGetVideosOpts().
    28  					ResourceOwnerId("test").
    29  					DeviceId("test"),
    30  				test: func() {
    31  					if query.Encode() != "device_id=test&resource_owner_id=test" {
    32  						t.Fatal("Missing Query", query.Encode())
    33  					} else {
    34  						t.Log(query.Encode())
    35  					}
    36  				},
    37  			},
    38  			{
    39  				params: srvvideo.MakeGetVideosOpts().
    40  					ResourceOwnerId("test").
    41  					DeviceId(""),
    42  				test: func() {
    43  					if query.Encode() != "resource_owner_id=test" {
    44  						t.Fatal("Missing Query", query.Encode())
    45  					} else {
    46  						t.Log(query.Encode())
    47  					}
    48  				},
    49  			},
    50  		}
    51  	)
    52  
    53  	// Query Test
    54  	responseHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    55  		query = r.URL.Query()
    56  		w.Header().Set("Content-Type", "application/json")
    57  		json.NewEncoder(w).Encode(cios.MultipleVideo{Total: 10})
    58  	})
    59  	ts := httptest.NewServer(responseHandler)
    60  	client := NewCiosClient(CiosClientConfig{Urls: sdkmodel.CIOSUrl{VideoStreamingUrl: ts.URL}})
    61  	defer ts.Close()
    62  	for _, test := range tests {
    63  		client.Video().GetVideoInfos(nil, test.params)
    64  		test.test()
    65  	}
    66  
    67  	ts.Close()
    68  }
    69  
    70  func TestVideo_GetVideoInfo(t *testing.T) {
    71  	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    72  		w.Header().Set("Content-Type", "application/json")
    73  		if r.URL.Path == "/v2/video_streams/test" {
    74  			json.NewEncoder(w).Encode(cios.SingleVideo{
    75  				Video: cios.Video{
    76  					Name:             nil,
    77  					Id:               "test",
    78  					DeviceId:         nil,
    79  					ResourceOwnerId:  "",
    80  					VideoName:        nil,
    81  					VideoDescription: nil,
    82  					Enabled:          nil,
    83  					CreatedAt:        "",
    84  					UpdatedAt:        "",
    85  				},
    86  			})
    87  		}
    88  	}))
    89  	defer ts.Close()
    90  	client := NewCiosClient(CiosClientConfig{Urls: sdkmodel.CIOSUrl{VideoStreamingUrl: ts.URL}})
    91  	responseB, response, err := client.Video().GetVideoInfo(nil, "test")
    92  	if responseB.Id != "test" || err != nil || response.StatusCode != 200 {
    93  		t.Fatal(responseB)
    94  	}
    95  }
    96  func TestVideo_GetThumbnail(t *testing.T) {
    97  	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    98  		w.Header().Set("Content-Type", "image/jpeg")
    99  		if r.URL.Path == "/v2/video_streams/test/thumbnail" {
   100  			if _, err := w.Write([]byte("test")); err != nil {
   101  				t.Error(err.Error())
   102  			}
   103  
   104  		}
   105  	}))
   106  	defer ts.Close()
   107  	client := NewCiosClient(CiosClientConfig{Urls: sdkmodel.CIOSUrl{VideoStreamingUrl: ts.URL}})
   108  	responseB, response, err := client.Video().GetThumbnail(nil, "test")
   109  	if string(responseB) != "test" || err != nil || response.StatusCode != 200 {
   110  		t.Fatal(responseB)
   111  
   112  	}
   113  }
   114  func TestVideo_UpdateVideoInfo(t *testing.T) {
   115  	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
   116  		w.Header().Set("Content-Type", "application/json")
   117  		var body cios.VideoUpdateRequest
   118  		if err := cnv.UnMarshalJson(r.Body, &body); err != nil {
   119  			t.Fatal(err.Error())
   120  		}
   121  		if *body.VideoName != "name" {
   122  			t.Fatal(body)
   123  		}
   124  		if *body.VideoDescription != "dp" {
   125  			t.Fatal(body)
   126  		}
   127  	}))
   128  	defer ts.Close()
   129  	client := NewCiosClient(CiosClientConfig{Urls: sdkmodel.CIOSUrl{VideoStreamingUrl: ts.URL}})
   130  	_, _, err := client.Video().UpdateVideoInfo(nil, "test", "name", "dp")
   131  	if err != nil {
   132  		t.Fatal(err.Error())
   133  	}
   134  }
   135  func TestVideo_Play(t *testing.T) {
   136  	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
   137  		w.Header().Set("Content-Type", "application/json")
   138  		if r.URL.Path != "/v2/video_streams/id/play" {
   139  			t.Fatal(r.URL.Path)
   140  		}
   141  		if r.Method != "POST" {
   142  			t.Fatal(r.Method)
   143  		}
   144  	}))
   145  	defer ts.Close()
   146  	client := NewCiosClient(CiosClientConfig{Urls: sdkmodel.CIOSUrl{VideoStreamingUrl: ts.URL}})
   147  	_, _, err := client.Video().Play(nil, "id")
   148  	if err != nil {
   149  		t.Fatal(err.Error())
   150  	}
   151  }
   152  func TestVideo_Stop(t *testing.T) {
   153  	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
   154  		w.Header().Set("Content-Type", "application/json")
   155  		if r.URL.Path != "/v2/video_streams/id/stop" {
   156  			t.Fatal(r.URL.Path)
   157  		}
   158  		if r.Method != "POST" {
   159  			t.Fatal(r.Method)
   160  		}
   161  	}))
   162  	defer ts.Close()
   163  	client := NewCiosClient(CiosClientConfig{Urls: sdkmodel.CIOSUrl{VideoStreamingUrl: ts.URL}})
   164  	_, err := client.Video().Stop(nil, "id")
   165  	if err != nil {
   166  		t.Fatal(err.Error())
   167  	}
   168  }