AlexaClientSDK  3.0.0
A cross-platform, modular SDK for interacting with the Alexa Voice Service
MockContentFetcher.h
Go to the documentation of this file.
1 /*
2  * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License").
5  * You may not use this file except in compliance with the License.
6  * A copy of the License is located at
7  *
8  * http://aws.amazon.com/apache2.0/
9  *
10  * or in the "license" file accompanying this file. This file is distributed
11  * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12  * express or implied. See the License for the specific language governing
13  * permissions and limitations under the License.
14  */
15 #ifndef ALEXA_CLIENT_SDK_PLAYLISTPARSER_TEST_PLAYLISTPARSER_MOCKCONTENTFETCHER_H_
16 #define ALEXA_CLIENT_SDK_PLAYLISTPARSER_TEST_PLAYLISTPARSER_MOCKCONTENTFETCHER_H_
17 
18 #include <string>
19 #include <vector>
20 #include <unordered_map>
21 
23 
24 namespace alexaClientSDK {
25 namespace playlistParser {
26 namespace test {
27 
28 using namespace avsCommon::sdkInterfaces;
29 
31 static const auto SHORT_TIMEOUT = std::chrono::milliseconds(100);
32 
34 static const std::string TEST_M3U_PLAYLIST_URL{"http://sanjayisthecoolest.com/sample.m3u"};
35 
37  "http://stream.radiotime.com/sample.mp3\n"
38  "http://live-mp3-128.kexp.org\n";
39 
40 static const size_t TEST_M3U_PLAYLIST_URL_EXPECTED_PARSES = 2;
41 
42 static const std::vector<std::chrono::milliseconds> TEST_M3U_DURATIONS = {std::chrono::milliseconds{-1},
43  std::chrono::milliseconds{-1}};
44 
45 static const std::vector<std::string> TEST_M3U_PLAYLIST_URLS = {"http://stream.radiotime.com/sample.mp3",
46  "http://live-mp3-128.kexp.org"};
47 
49 static const std::string TEST_M3U_RELATIVE_PLAYLIST_URL{"http://sanjayisthecoolest.com/sampleRelativeUrls.m3u"};
50 
52  "../test.mp3\n"
53  "test2.aac\n";
54 
56 
57 static const std::vector<std::string> TEST_M3U_RELATIVE_PLAYLIST_URLS = {"http://sanjayisthecoolest.com/../test.mp3",
58  "http://sanjayisthecoolest.com/test2.aac"};
59 
61 static const std::string TEST_HLS_PLAYLIST_URL{"http://sanjayisthecoolest.com/sample.m3u8"};
62 
64  "#EXTM3U\n"
65  "#EXT-X-TARGETDURATION:10\n"
66  "#EXT-X-MEDIA-SEQUENCE:9684358\n"
67  "#EXTINF:10,RADIO\n"
68  "http://76.74.255.139/bismarck/live/bismarck.mov_9684358.aac\n"
69  "#EXTINF:10.0,RADIO\n"
70  "http://76.74.255.139/bismarck/live/bismarck.mov_9684359.aac\n"
71  "#EXTINF:10,RADIO\n"
72  "http://76.74.255.139/bismarck/live/bismarck.mov_9684360.aac\n"
73  "#EXTINF:10.34,RADIO\n"
74  "http://76.74.255.139/bismarck/live/bismarck.mov_9684360.aac\n"
75  "#EXTINF:10.344,RADIO\n"
76  "http://76.74.255.139/bismarck/live/bismarck.mov_9684360.aac\n"
77  "#EXTINF:10.3444,RADIO\n"
78  "http://76.74.255.139/bismarck/live/bismarck.mov_9684360.aac\n"
79  "#EXTINF:10.002,RADIO\n"
80  "http://76.74.255.139/bismarck/live/bismarck.mov_9684360.aac\n"
81  "#EXTINF:10.0022,RADIO\n"
82  "http://76.74.255.139/bismarck/live/bismarck.mov_9684360.aac\n"
83  "#EXTINF : 10.0022,RADIO\n"
84  "http://76.74.255.139/bismarck/live/bismarck.mov_9684360.aac\n"
85  "#EXTINF : -1,RADIO\n"
86  "http://76.74.255.139/bismarck/live/bismarck.mov_9684360.aac\n"
87  "#EXTINF:123ms,RADIO\n"
88  "http://76.74.255.139/bismarck/live/bismarck.mov_9684360.aac\n"
89  "#EXTINF:123 ms,RADIO\n"
90  "http://76.74.255.139/bismarck/live/bismarck.mov_9684360.aac\n"
91  "#EXTINF:123.0ms,RADIO\n"
92  "http://76.74.255.139/bismarck/live/bismarck.mov_9684360.aac\n"
93  "#EXTINF:123ms,\n"
94  "http://76.74.255.139/bismarck/live/bismarck.mov_9684360.aac\n"
95  "#EXTINF:123 ,hi\n"
96  "http://76.74.255.139/bismarck/live/bismarck.mov_9684360.aac\n"
97  "#EXT-X-ENDLIST\n";
98 
99 static const size_t TEST_HLS_PLAYLIST_URL_EXPECTED_PARSES = 15;
100 
101 static const std::vector<std::string> TEST_HLS_PLAYLIST_URLS = {
102  "http://76.74.255.139/bismarck/live/bismarck.mov_9684358.aac",
103  "http://76.74.255.139/bismarck/live/bismarck.mov_9684359.aac",
104  "http://76.74.255.139/bismarck/live/bismarck.mov_9684360.aac",
105  "http://76.74.255.139/bismarck/live/bismarck.mov_9684360.aac",
106  "http://76.74.255.139/bismarck/live/bismarck.mov_9684360.aac",
107  "http://76.74.255.139/bismarck/live/bismarck.mov_9684360.aac",
108  "http://76.74.255.139/bismarck/live/bismarck.mov_9684360.aac",
109  "http://76.74.255.139/bismarck/live/bismarck.mov_9684360.aac",
110  "http://76.74.255.139/bismarck/live/bismarck.mov_9684360.aac",
111  "http://76.74.255.139/bismarck/live/bismarck.mov_9684360.aac",
112  "http://76.74.255.139/bismarck/live/bismarck.mov_9684360.aac",
113  "http://76.74.255.139/bismarck/live/bismarck.mov_9684360.aac",
114  "http://76.74.255.139/bismarck/live/bismarck.mov_9684360.aac",
115  "http://76.74.255.139/bismarck/live/bismarck.mov_9684360.aac",
116  "http://76.74.255.139/bismarck/live/bismarck.mov_9684360.aac"};
117 
118 static const std::vector<std::chrono::milliseconds> TEST_HLS_DURATIONS = {std::chrono::milliseconds{10000},
119  std::chrono::milliseconds{10000},
120  std::chrono::milliseconds{10000},
121  std::chrono::milliseconds{10340},
122  std::chrono::milliseconds{10344},
123  std::chrono::milliseconds{10344},
124  std::chrono::milliseconds{10002},
125  std::chrono::milliseconds{10002},
126  std::chrono::milliseconds{10002},
127  std::chrono::milliseconds{-1},
128  std::chrono::milliseconds{-1},
129  std::chrono::milliseconds{-1},
130  std::chrono::milliseconds{-1},
131  std::chrono::milliseconds{-1},
132  std::chrono::milliseconds{123000}};
133 
135 static const std::string TEST_PLS_PLAYLIST_URL{"http://sanjayisthecoolest.com/sample3.pls"};
136 
138  "[playlist]\n"
139  "NumberOfEntries=2\n"
140  "File1=http://stream.radiotime.com/sample.mp3\n"
141  "Length1=-1\n"
142  "File2=http://live-mp3-128.kexp.org\n"
143  "Length2=-1\n";
144 
146 
147 static const std::vector<std::string> TEST_PLS_PLAYLIST_URLS = {"http://stream.radiotime.com/sample.mp3",
148  "http://live-mp3-128.kexp.org"};
149 
151 static const std::string TEST_HLS_LIVE_STREAM_PLAYLIST_URL{"http://sanjayisthecoolest.com/liveStream.m3u8"};
152 
154  "#EXTM3U\n"
155  "#EXT-X-TARGETDURATION:10\n"
156  "#EXT-X-MEDIA-SEQUENCE:9684358\n"
157  "#EXTINF:10,RADIO\n"
158  "http://76.74.255.139/bismarck/live/bismarck.mov_9684358.aac\n"
159  "#EXTINF:10.0,RADIO\n"
160  "http://76.74.255.139/bismarck/live/bismarck.mov_9684359.aac\n"
161  "#EXTINF:10,RADIO\n"
162  "http://76.74.255.139/bismarck/live/bismarck.mov_9684360.aac\n";
163 
165  "#EXTM3U\n"
166  "#EXT-X-TARGETDURATION:10\n"
167  "#EXT-X-MEDIA-SEQUENCE:9684360\n"
168  "#EXTINF:10,RADIO\n"
169  "http://76.74.255.139/bismarck/live/bismarck.mov_9684360.aac\n"
170  "#EXTINF:10,RADIO\n"
171  "http://sanjay.com/chunk.mp3\n"
172  "#EXTINF:10,RADIO\n"
173  "http://sanjay.com/anotherChunk.mp3\n"
174  "#EXT-X-ENDLIST\n";
175 
177 
178 static const std::vector<std::string> TEST_HLS_LIVE_STREAM_PLAYLIST_URLS = {
179  "http://76.74.255.139/bismarck/live/bismarck.mov_9684358.aac",
180  "http://76.74.255.139/bismarck/live/bismarck.mov_9684359.aac",
181  "http://76.74.255.139/bismarck/live/bismarck.mov_9684360.aac",
182  "http://sanjay.com/chunk.mp3",
183  "http://sanjay.com/anotherChunk.mp3"};
184 
185 static const std::vector<std::chrono::milliseconds> TEST_HLS_LIVE_STREAM_DURATIONS = {std::chrono::milliseconds{10000},
186  std::chrono::milliseconds{10000},
187  std::chrono::milliseconds{10000},
188  std::chrono::milliseconds{10000},
189  std::chrono::milliseconds{10000}};
190 
191 static const size_t NUM_PARSES_EXPECTED_WHEN_NO_PARSING = 1;
192 
193 static const std::string TEST_MEDIA_URL = "https://sanjay.com/some_media.mp3";
194 
195 static const std::unordered_map<std::string, std::string> urlsToContentTypes{
196  // Valid playlist content types
197  {TEST_M3U_PLAYLIST_URL, "audio/mpegurl"},
198  {TEST_M3U_RELATIVE_PLAYLIST_URL, "audio/mpegurl"},
199  {TEST_HLS_PLAYLIST_URL, "application/vnd.apple.mpegurl"},
200  {TEST_PLS_PLAYLIST_URL, "audio/x-scpls"},
201  {TEST_HLS_LIVE_STREAM_PLAYLIST_URL, "audio/mpegurl"},
202  // Not playlist content types
203  {TEST_MEDIA_URL, "audio/mpeg"},
204  {"http://stream.radiotime.com/sample.mp3", "audio/mpeg"},
205  {"http://live-mp3-128.kexp.org", "audio/mpeg"},
206  {"http://76.74.255.139/bismarck/live/bismarck.mov_9684358.aac", "audio/mpeg"},
207  {"http://76.74.255.139/bismarck/live/bismarck.mov_9684359.aac", "audio/mpeg"},
208  {"http://76.74.255.139/bismarck/live/bismarck.mov_9684360.aac", "audio/mpeg"},
209  {"http://stream.radiotime.com/sample.mp3", "audio/mpeg"},
210  {"http://live-mp3-128.kexp.org", "audio/mpeg"},
211  {"http://sanjayisthecoolest.com/../test.mp3", "audio/mpeg"},
212  {"http://sanjayisthecoolest.com/test2.aac", "audio/mpeg"},
213  {"http://sanjay.com/chunk.mp3", "audio/mpeg"},
214  {"http://sanjay.com/anotherChunk.mp3", "audio/mpeg"}};
215 
216 static std::unordered_map<std::string, std::string> urlsToContent{
217  {TEST_M3U_PLAYLIST_URL, TEST_M3U_PLAYLIST_CONTENT},
218  {TEST_M3U_RELATIVE_PLAYLIST_URL, TEST_M3U_RELATIVE_PLAYLIST_CONTENT},
219  {TEST_HLS_PLAYLIST_URL, TEST_HLS_PLAYLIST_CONTENT},
220  {TEST_PLS_PLAYLIST_URL, TEST_PLS_CONTENT},
221  {TEST_HLS_LIVE_STREAM_PLAYLIST_URL, TEST_HLS_LIVE_STREAM_PLAYLIST_CONTENT_1}};
222 
225 public:
226  explicit MockContentFetcher(const std::string& url) :
227  m_url{url},
228  m_state{HTTPContentFetcherInterface::State::INITIALIZED} {
229  }
230 
231  std::string getUrl() const override {
232  return m_url;
233  }
234 
235  std::string getEffectiveUrl() const override {
236  return m_url;
237  }
238 
239  HTTPContentFetcherInterface::Header getHeader(std::atomic<bool>* shouldShutdown) override {
241  auto it1 = urlsToContentTypes.find(m_url);
242  if (it1 == urlsToContentTypes.end()) {
243  header.successful = false;
244  } else {
245  header.successful = true;
247  header.contentType = it1->second;
248  m_state = HTTPContentFetcherInterface::State::HEADER_DONE;
249  }
250  return header;
251  }
252 
254  return m_state;
255  }
256 
257  bool getBody(std::shared_ptr<avsCommon::avs::attachment::AttachmentWriter> writer) override {
258  auto it2 = urlsToContent.find(m_url);
259  if (it2 == urlsToContent.end()) {
260  return false;
261  } else {
262  static bool liveStreamPlaylistRequested = false;
263  if (m_url == TEST_HLS_LIVE_STREAM_PLAYLIST_URL) {
264  if (!liveStreamPlaylistRequested) {
266  liveStreamPlaylistRequested = true;
267  } else {
269  }
270  }
271  auto attachment = writeStringIntoAttachment(it2->second, std::move(writer));
272  if (!attachment) {
273  return false;
274  }
275  m_state = HTTPContentFetcherInterface::State::BODY_DONE;
276  }
277  return true;
278  }
279 
280  void shutdown() override {
281  }
282 
283  std::unique_ptr<avsCommon::utils::HTTPContent> getContent(
284  FetchOptions fetchOption,
285  std::unique_ptr<avsCommon::avs::attachment::AttachmentWriter> writer,
286  const std::vector<std::string>& customHeaders = std::vector<std::string>()) override {
287  return nullptr;
288  }
289 
290 private:
291  std::shared_ptr<avsCommon::avs::attachment::InProcessAttachment> writeStringIntoAttachment(
292  const std::string& string,
293  std::shared_ptr<avsCommon::avs::attachment::AttachmentWriter> writer) {
294  static int id = 0;
295  std::shared_ptr<avsCommon::avs::attachment::InProcessAttachment> stream =
296  std::make_shared<avsCommon::avs::attachment::InProcessAttachment>(std::to_string(id++));
297  if (!stream) {
298  return nullptr;
299  }
300 
301  if (!writer) {
302  writer = stream->createWriter();
303  }
305  writer->write(string.data(), string.size(), &writeStatus);
306  return stream;
307  };
308 
309  std::string m_url;
310 
312 };
313 
314 } // namespace test
315 } // namespace playlistParser
316 } // namespace alexaClientSDK
317 
318 #endif // ALEXA_CLIENT_SDK_PLAYLISTPARSER_TEST_PLAYLISTPARSER_MOCKCONTENTFETCHER_H_
static const size_t TEST_M3U_RELATIVE_PLAYLIST_URL_EXPECTED_PARSES
Definition: MockContentFetcher.h:55
HTTPContentFetcherInterface::Header getHeader(std::atomic< bool > *shouldShutdown) override
Definition: MockContentFetcher.h:239
static const std::vector< std::string > TEST_M3U_RELATIVE_PLAYLIST_URLS
Definition: MockContentFetcher.h:57
static const std::vector< std::string > TEST_PLS_PLAYLIST_URLS
Definition: MockContentFetcher.h:147
static const std::string TEST_HLS_LIVE_STREAM_PLAYLIST_URL
A test playlist in HLS format.
Definition: MockContentFetcher.h:151
Definition: AmdMetricWrapperTest.cpp:24
::std::string string
Definition: gtest-port.h:1097
std::string getEffectiveUrl() const override
Definition: MockContentFetcher.h:235
static const size_t TEST_PLS_PLAYLIST_URL_EXPECTED_PARSES
Definition: MockContentFetcher.h:145
std::unique_ptr< avsCommon::utils::HTTPContent > getContent(FetchOptions fetchOption, std::unique_ptr< avsCommon::avs::attachment::AttachmentWriter > writer, const std::vector< std::string > &customHeaders=std::vector< std::string >()) override
Definition: MockContentFetcher.h:283
static const std::string TEST_HLS_PLAYLIST_URL
A test playlist in HLS format.
Definition: MockContentFetcher.h:61
bool getBody(std::shared_ptr< avsCommon::avs::attachment::AttachmentWriter > writer) override
Definition: MockContentFetcher.h:257
static const std::string TEST_HLS_LIVE_STREAM_PLAYLIST_CONTENT_2
Definition: MockContentFetcher.h:164
A mock content fetcher.
Definition: MockContentFetcher.h:224
avsCommon::utils::http::HTTPResponseCode responseCode
The HTTP status code received.
Definition: HTTPContentFetcherInterface.h:68
static const std::string TEST_PLS_PLAYLIST_URL
A test playlist in PLS format.
Definition: MockContentFetcher.h:135
static const std::vector< std::string > TEST_M3U_PLAYLIST_URLS
Definition: MockContentFetcher.h:45
static const std::string TEST_PLS_CONTENT
Definition: MockContentFetcher.h:137
static const size_t TEST_M3U_PLAYLIST_URL_EXPECTED_PARSES
Definition: MockContentFetcher.h:40
static const std::vector< std::string > TEST_HLS_LIVE_STREAM_PLAYLIST_URLS
Definition: MockContentFetcher.h:178
HTTP Success with response payload.
Definition: HttpResponseCodes.h:31
static const std::string TEST_HLS_LIVE_STREAM_PLAYLIST_CONTENT_1
Definition: MockContentFetcher.h:153
static const std::string TEST_M3U_RELATIVE_PLAYLIST_CONTENT
Definition: MockContentFetcher.h:51
static const std::unordered_map< std::string, std::string > urlsToContentTypes
Definition: MockContentFetcher.h:195
static const std::string TEST_HLS_PLAYLIST_CONTENT
Definition: MockContentFetcher.h:63
static const size_t NUM_PARSES_EXPECTED_WHEN_NO_PARSING
Definition: MockContentFetcher.h:191
State
The state of payload fetching.
Definition: HTTPContentFetcherInterface.h:43
MockContentFetcher(const std::string &url)
Definition: MockContentFetcher.h:226
static const std::string TEST_M3U_PLAYLIST_URL
Test M3U url.
Definition: MockContentFetcher.h:34
static const std::string TEST_M3U_PLAYLIST_CONTENT
Definition: MockContentFetcher.h:36
static const std::vector< std::chrono::milliseconds > TEST_M3U_DURATIONS
Definition: MockContentFetcher.h:42
static const std::string TEST_M3U_RELATIVE_PLAYLIST_URL
Test M3U url with relative urls within.
Definition: MockContentFetcher.h:49
Whether or not curl logs should be emitted.
Definition: AVSConnectionManager.h:36
std::string getUrl() const override
Definition: MockContentFetcher.h:231
FetchOptions
Represents what HTTP content to fetch.
Definition: HTTPContentFetcherInterface.h:35
static const std::vector< std::chrono::milliseconds > TEST_HLS_LIVE_STREAM_DURATIONS
Definition: MockContentFetcher.h:185
HTTPContentFetcherInterface::State getState() override
Definition: MockContentFetcher.h:253
static const std::vector< std::chrono::milliseconds > TEST_HLS_DURATIONS
Definition: MockContentFetcher.h:118
static const auto SHORT_TIMEOUT
Short time out for when callbacks are expected not to occur.
Definition: MockContentFetcher.h:31
static std::unordered_map< std::string, std::string > urlsToContent
Definition: MockContentFetcher.h:216
std::string contentType
The value of the Content-Type HTTP header.
Definition: HTTPContentFetcherInterface.h:70
static const size_t TEST_HLS_LIVE_STREAM_PLAYLIST_EXPECTED_PARSES
Definition: MockContentFetcher.h:176
static const size_t TEST_HLS_PLAYLIST_URL_EXPECTED_PARSES
Definition: MockContentFetcher.h:99
const T & move(const T &t)
Definition: gtest-port.h:1317
static const std::string TEST_MEDIA_URL
Definition: MockContentFetcher.h:193
static const std::vector< std::string > TEST_HLS_PLAYLIST_URLS
Definition: MockContentFetcher.h:101
void shutdown() override
Definition: MockContentFetcher.h:280

AlexaClientSDK 3.0.0 - Copyright 2016-2022 Amazon.com, Inc. or its affiliates. All Rights Reserved. Licensed under the Apache License, Version 2.0