aboutsummaryrefslogtreecommitdiffhomepage
path: root/tensorflow/core/platform/cloud/http_request_test.cc
blob: 93c4ec51d955a114d07cc8487bc07bb7cce103d4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
/* Copyright 2016 The TensorFlow Authors. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/

#include "tensorflow/core/platform/cloud/http_request.h"
#include <fstream>
#include "tensorflow/core/lib/core/status_test_util.h"
#include "tensorflow/core/lib/io/path.h"
#include "tensorflow/core/platform/test.h"

namespace tensorflow {
namespace {

const string kTestContent = "random original scratch content";

// A fake proxy that pretends to be libcurl.
class FakeLibCurl : public LibCurl {
 public:
  FakeLibCurl(const string& response_content, uint64 response_code)
      : response_content(response_content), response_code(response_code) {}
  FakeLibCurl(const string& response_content, uint64 response_code,
              const std::vector<string>& response_headers)
      : response_content(response_content),
        response_code(response_code),
        response_headers(response_headers) {}
  CURL* curl_easy_init() override {
    is_initialized = true;
    // The reuslt just needs to be non-null.
    return reinterpret_cast<CURL*>(this);
  }
  CURLcode curl_easy_setopt(CURL* curl, CURLoption option,
                            uint64 param) override {
    switch (option) {
      case CURLOPT_POST:
        is_post = param;
        break;
      case CURLOPT_PUT:
        is_put = param;
        break;
      default:
        break;
    }
    return CURLE_OK;
  }
  CURLcode curl_easy_setopt(CURL* curl, CURLoption option,
                            const char* param) override {
    return curl_easy_setopt(curl, option,
                            reinterpret_cast<void*>(const_cast<char*>(param)));
  }
  CURLcode curl_easy_setopt(CURL* curl, CURLoption option,
                            void* param) override {
    switch (option) {
      case CURLOPT_URL:
        url = reinterpret_cast<char*>(param);
        break;
      case CURLOPT_RANGE:
        range = reinterpret_cast<char*>(param);
        break;
      case CURLOPT_CUSTOMREQUEST:
        custom_request = reinterpret_cast<char*>(param);
        break;
      case CURLOPT_HTTPHEADER:
        headers = reinterpret_cast<std::vector<string>*>(param);
        break;
      case CURLOPT_ERRORBUFFER:
        error_buffer = reinterpret_cast<char*>(param);
        break;
      case CURLOPT_WRITEDATA:
        write_data = reinterpret_cast<FILE*>(param);
        break;
      case CURLOPT_HEADERDATA:
        header_data = reinterpret_cast<FILE*>(param);
        break;
      case CURLOPT_READDATA:
        read_data = reinterpret_cast<FILE*>(param);
        break;
      default:
        break;
    }
    return CURLE_OK;
  }
  CURLcode curl_easy_setopt(CURL* curl, CURLoption option,
                            size_t (*param)(void*, size_t, size_t,
                                            FILE*)) override {
    read_callback = param;
    return CURLE_OK;
  }
  CURLcode curl_easy_setopt(CURL* curl, CURLoption option,
                            size_t (*param)(const void*, size_t, size_t,
                                            void*)) override {
    switch (option) {
      case CURLOPT_WRITEFUNCTION:
        write_callback = param;
        break;
      case CURLOPT_HEADERFUNCTION:
        header_callback = param;
        break;
      default:
        break;
    }
    return CURLE_OK;
  }
  CURLcode curl_easy_perform(CURL* curl) override {
    if (read_data) {
      char buffer[3];
      int bytes_read;
      posted_content = "";
      do {
        bytes_read = read_callback(buffer, 1, sizeof(buffer), read_data);
        posted_content =
            strings::StrCat(posted_content, StringPiece(buffer, bytes_read));
      } while (bytes_read > 0);
    }
    if (write_data) {
      write_callback(response_content.c_str(), 1, response_content.size(),
                     write_data);
    }
    for (const auto& header : response_headers) {
      header_callback(header.c_str(), 1, header.size(), header_data);
    }
    return curl_easy_perform_result;
  }
  CURLcode curl_easy_getinfo(CURL* curl, CURLINFO info,
                             uint64* value) override {
    switch (info) {
      case CURLINFO_RESPONSE_CODE:
        *value = response_code;
        break;
      default:
        break;
    }
    return CURLE_OK;
  }
  CURLcode curl_easy_getinfo(CURL* curl, CURLINFO info,
                             double* value) override {
    switch (info) {
      case CURLINFO_SIZE_DOWNLOAD:
        *value = response_content.size();
        break;
      default:
        break;
    }
    return CURLE_OK;
  }
  void curl_easy_cleanup(CURL* curl) override { is_cleaned_up = true; }
  curl_slist* curl_slist_append(curl_slist* list, const char* str) override {
    std::vector<string>* v = list ? reinterpret_cast<std::vector<string>*>(list)
                                  : new std::vector<string>();
    v->push_back(str);
    return reinterpret_cast<curl_slist*>(v);
  }
  char* curl_easy_escape(CURL* curl, const char* str, int length) override {
    // This function just does a simple replacing of "/" with "%2F" instead of
    // full url encoding.
    const string victim = "/";
    const string encoded = "%2F";

    string temp_str = str;
    std::string::size_type n = 0;
    while ((n = temp_str.find(victim, n)) != std::string::npos) {
      temp_str.replace(n, victim.size(), encoded);
      n += encoded.size();
    }
    char* out_char_str = (char*)malloc(sizeof(char) * temp_str.size() + 1);
    std::copy(temp_str.begin(), temp_str.end(), out_char_str);
    out_char_str[temp_str.size()] = '\0';
    return out_char_str;
  }
  void curl_slist_free_all(curl_slist* list) override {
    delete reinterpret_cast<std::vector<string>*>(list);
  }
  void curl_free(void* p) override { free(p); }

  // Variables defining the behavior of this fake.
  string response_content;
  uint64 response_code;
  std::vector<string> response_headers;

  // Internal variables to store the libcurl state.
  string url;
  string range;
  string custom_request;
  char* error_buffer = nullptr;
  bool is_initialized = false;
  bool is_cleaned_up = false;
  std::vector<string>* headers = nullptr;
  bool is_post = false;
  bool is_put = false;
  void* write_data = nullptr;
  size_t (*write_callback)(const void* ptr, size_t size, size_t nmemb,
                           void* userdata) = nullptr;
  void* header_data = nullptr;
  size_t (*header_callback)(const void* ptr, size_t size, size_t nmemb,
                            void* userdata) = nullptr;
  FILE* read_data = nullptr;
  size_t (*read_callback)(void* ptr, size_t size, size_t nmemb,
                          FILE* userdata) = &fread;
  // Outcome of performing the request.
  string posted_content;
  CURLcode curl_easy_perform_result = CURLE_OK;
};

TEST(HttpRequestTest, GetRequest) {
  FakeLibCurl libcurl("get response", 200);
  HttpRequest http_request(&libcurl);
  TF_EXPECT_OK(http_request.Init());

  std::vector<char> scratch;
  scratch.insert(scratch.begin(), kTestContent.begin(), kTestContent.end());
  StringPiece result;
  scratch.reserve(100);

  TF_EXPECT_OK(http_request.SetUri("http://www.testuri.com"));
  TF_EXPECT_OK(http_request.AddAuthBearerHeader("fake-bearer"));
  TF_EXPECT_OK(http_request.SetRange(100, 199));
  TF_EXPECT_OK(http_request.SetResultBuffer(&scratch));
  TF_EXPECT_OK(http_request.Send());

  EXPECT_EQ("get response", string(scratch.begin(), scratch.end()));

  // Check interactions with libcurl.
  EXPECT_TRUE(libcurl.is_initialized);
  EXPECT_EQ("http://www.testuri.com", libcurl.url);
  EXPECT_EQ("100-199", libcurl.range);
  EXPECT_EQ("", libcurl.custom_request);
  EXPECT_EQ(1, libcurl.headers->size());
  EXPECT_EQ("Authorization: Bearer fake-bearer", (*libcurl.headers)[0]);
  EXPECT_FALSE(libcurl.is_post);
  EXPECT_EQ(200, http_request.GetResponseCode());
}

TEST(HttpRequestTest, GetRequest_Empty) {
  FakeLibCurl libcurl("", 200);
  HttpRequest http_request(&libcurl);
  TF_EXPECT_OK(http_request.Init());

  std::vector<char> scratch;
  scratch.resize(0);

  TF_EXPECT_OK(http_request.SetUri("http://www.testuri.com"));
  TF_EXPECT_OK(http_request.AddAuthBearerHeader("fake-bearer"));
  TF_EXPECT_OK(http_request.SetRange(100, 199));
  TF_EXPECT_OK(http_request.SetResultBuffer(&scratch));
  TF_EXPECT_OK(http_request.Send());

  EXPECT_TRUE(scratch.empty());

  // Check interactions with libcurl.
  EXPECT_TRUE(libcurl.is_initialized);
  EXPECT_EQ("http://www.testuri.com", libcurl.url);
  EXPECT_EQ("100-199", libcurl.range);
  EXPECT_EQ("", libcurl.custom_request);
  EXPECT_EQ(1, libcurl.headers->size());
  EXPECT_EQ("Authorization: Bearer fake-bearer", (*libcurl.headers)[0]);
  EXPECT_FALSE(libcurl.is_post);
  EXPECT_EQ(200, http_request.GetResponseCode());
}

TEST(HttpRequestTest, GetRequest_RangeOutOfBound) {
  FakeLibCurl libcurl("get response", 416);
  libcurl.curl_easy_perform_result = CURLE_WRITE_ERROR;
  HttpRequest http_request(&libcurl);
  TF_EXPECT_OK(http_request.Init());

  std::vector<char> scratch;
  scratch.insert(scratch.end(), kTestContent.begin(), kTestContent.end());

  TF_EXPECT_OK(http_request.SetUri("http://www.testuri.com"));
  TF_EXPECT_OK(http_request.AddAuthBearerHeader("fake-bearer"));
  TF_EXPECT_OK(http_request.SetRange(100, 199));
  TF_EXPECT_OK(http_request.SetResultBuffer(&scratch));
  TF_EXPECT_OK(http_request.Send());

  EXPECT_TRUE(scratch.empty());
  EXPECT_EQ(416, http_request.GetResponseCode());
}

TEST(HttpRequestTest, GetRequest_503) {
  FakeLibCurl libcurl("get response", 503);
  libcurl.curl_easy_perform_result = CURLE_WRITE_ERROR;
  HttpRequest http_request(&libcurl);
  TF_EXPECT_OK(http_request.Init());

  std::vector<char> scratch;
  scratch.insert(scratch.end(), kTestContent.begin(), kTestContent.end());

  TF_EXPECT_OK(http_request.SetUri("http://www.testuri.com"));
  TF_EXPECT_OK(http_request.AddAuthBearerHeader("fake-bearer"));
  TF_EXPECT_OK(http_request.SetRange(100, 199));
  TF_EXPECT_OK(http_request.SetResultBuffer(&scratch));
  EXPECT_EQ(error::UNAVAILABLE, http_request.Send().code());
  EXPECT_EQ(503, http_request.GetResponseCode());
}

TEST(HttpRequestTest, ResponseHeaders) {
  FakeLibCurl libcurl(
      "get response", 200,
      {"Location: abcd", "Content-Type: text", "unparsable header"});
  HttpRequest http_request(&libcurl);
  TF_EXPECT_OK(http_request.Init());

  TF_EXPECT_OK(http_request.SetUri("http://www.testuri.com"));
  TF_EXPECT_OK(http_request.Send());

  EXPECT_EQ("abcd", http_request.GetResponseHeader("Location"));
  EXPECT_EQ("text", http_request.GetResponseHeader("Content-Type"));
  EXPECT_EQ("", http_request.GetResponseHeader("Not-Seen-Header"));
}

TEST(HttpRequestTest, PutRequest_WithBody_FromFile) {
  FakeLibCurl libcurl("", 200);
  HttpRequest http_request(&libcurl);
  TF_EXPECT_OK(http_request.Init());

  auto content_filename = io::JoinPath(testing::TmpDir(), "content");
  std::ofstream content(content_filename, std::ofstream::binary);
  content << "post body content";
  content.close();

  TF_EXPECT_OK(http_request.SetUri("http://www.testuri.com"));
  TF_EXPECT_OK(http_request.AddAuthBearerHeader("fake-bearer"));
  TF_EXPECT_OK(http_request.SetPutFromFile(content_filename, 0));
  TF_EXPECT_OK(http_request.Send());

  // Check interactions with libcurl.
  EXPECT_TRUE(libcurl.is_initialized);
  EXPECT_EQ("http://www.testuri.com", libcurl.url);
  EXPECT_EQ("", libcurl.custom_request);
  EXPECT_EQ(2, libcurl.headers->size());
  EXPECT_EQ("Authorization: Bearer fake-bearer", (*libcurl.headers)[0]);
  EXPECT_EQ("Content-Length: 17", (*libcurl.headers)[1]);
  EXPECT_TRUE(libcurl.is_put);
  EXPECT_EQ("post body content", libcurl.posted_content);

  std::remove(content_filename.c_str());
}

TEST(HttpRequestTest, PutRequest_WithBody_FromFile_NonZeroOffset) {
  FakeLibCurl libcurl("", 200);
  HttpRequest http_request(&libcurl);
  TF_EXPECT_OK(http_request.Init());

  auto content_filename = io::JoinPath(testing::TmpDir(), "content");
  std::ofstream content(content_filename, std::ofstream::binary);
  content << "post body content";
  content.close();

  TF_EXPECT_OK(http_request.SetUri("http://www.testuri.com"));
  TF_EXPECT_OK(http_request.AddAuthBearerHeader("fake-bearer"));
  TF_EXPECT_OK(http_request.SetPutFromFile(content_filename, 7));
  TF_EXPECT_OK(http_request.Send());

  // Check interactions with libcurl.
  EXPECT_EQ("dy content", libcurl.posted_content);

  std::remove(content_filename.c_str());
}

TEST(HttpRequestTest, PostRequest_WithBody_FromMemory) {
  FakeLibCurl libcurl("", 200);
  HttpRequest http_request(&libcurl);
  TF_EXPECT_OK(http_request.Init());

  string content = "post body content";

  TF_EXPECT_OK(http_request.SetUri("http://www.testuri.com"));
  TF_EXPECT_OK(http_request.AddAuthBearerHeader("fake-bearer"));
  TF_EXPECT_OK(http_request.SetPostFromBuffer(content.c_str(), content.size()));
  TF_EXPECT_OK(http_request.Send());

  // Check interactions with libcurl.
  EXPECT_TRUE(libcurl.is_initialized);
  EXPECT_EQ("http://www.testuri.com", libcurl.url);
  EXPECT_EQ("", libcurl.custom_request);
  EXPECT_EQ(2, libcurl.headers->size());
  EXPECT_EQ("Authorization: Bearer fake-bearer", (*libcurl.headers)[0]);
  EXPECT_EQ("Content-Length: 17", (*libcurl.headers)[1]);
  EXPECT_TRUE(libcurl.is_post);
  EXPECT_EQ("post body content", libcurl.posted_content);
}

TEST(HttpRequestTest, PostRequest_WithoutBody) {
  FakeLibCurl libcurl("", 200);
  HttpRequest http_request(&libcurl);
  TF_EXPECT_OK(http_request.Init());

  TF_EXPECT_OK(http_request.SetUri("http://www.testuri.com"));
  TF_EXPECT_OK(http_request.AddAuthBearerHeader("fake-bearer"));
  TF_EXPECT_OK(http_request.SetPostEmptyBody());
  TF_EXPECT_OK(http_request.Send());

  // Check interactions with libcurl.
  EXPECT_TRUE(libcurl.is_initialized);
  EXPECT_EQ("http://www.testuri.com", libcurl.url);
  EXPECT_EQ("", libcurl.custom_request);
  EXPECT_EQ(2, libcurl.headers->size());
  EXPECT_EQ("Authorization: Bearer fake-bearer", (*libcurl.headers)[0]);
  EXPECT_EQ("Content-Length: 0", (*libcurl.headers)[1]);
  EXPECT_TRUE(libcurl.is_post);
  EXPECT_EQ("", libcurl.posted_content);
}

TEST(HttpRequestTest, DeleteRequest) {
  FakeLibCurl libcurl("", 200);
  HttpRequest http_request(&libcurl);
  TF_EXPECT_OK(http_request.Init());

  TF_EXPECT_OK(http_request.SetUri("http://www.testuri.com"));
  TF_EXPECT_OK(http_request.AddAuthBearerHeader("fake-bearer"));
  TF_EXPECT_OK(http_request.SetDeleteRequest());
  TF_EXPECT_OK(http_request.Send());

  // Check interactions with libcurl.
  EXPECT_TRUE(libcurl.is_initialized);
  EXPECT_EQ("http://www.testuri.com", libcurl.url);
  EXPECT_EQ("DELETE", libcurl.custom_request);
  EXPECT_EQ(1, libcurl.headers->size());
  EXPECT_EQ("Authorization: Bearer fake-bearer", (*libcurl.headers)[0]);
  EXPECT_FALSE(libcurl.is_post);
}

TEST(HttpRequestTest, WrongSequenceOfCalls_NoUri) {
  FakeLibCurl libcurl("", 200);
  HttpRequest http_request(&libcurl);
  TF_EXPECT_OK(http_request.Init());

  auto s = http_request.Send();
  ASSERT_TRUE(errors::IsFailedPrecondition(s));
  EXPECT_TRUE(StringPiece(s.error_message()).contains("URI has not been set"));
}

TEST(HttpRequestTest, WrongSequenceOfCalls_TwoSends) {
  FakeLibCurl libcurl("", 200);
  HttpRequest http_request(&libcurl);
  TF_EXPECT_OK(http_request.Init());

  http_request.SetUri("http://www.google.com");
  http_request.Send();
  auto s = http_request.Send();
  ASSERT_TRUE(errors::IsFailedPrecondition(s));
  EXPECT_TRUE(StringPiece(s.error_message())
                  .contains("The request has already been sent"));
}

TEST(HttpRequestTest, WrongSequenceOfCalls_ReusingAfterSend) {
  FakeLibCurl libcurl("", 200);
  HttpRequest http_request(&libcurl);
  TF_EXPECT_OK(http_request.Init());

  http_request.SetUri("http://www.google.com");
  http_request.Send();
  auto s = http_request.SetUri("http://mail.google.com");
  ASSERT_TRUE(errors::IsFailedPrecondition(s));
  EXPECT_TRUE(StringPiece(s.error_message())
                  .contains("The request has already been sent"));
}

TEST(HttpRequestTest, WrongSequenceOfCalls_SettingMethodTwice) {
  FakeLibCurl libcurl("", 200);
  HttpRequest http_request(&libcurl);
  TF_EXPECT_OK(http_request.Init());

  http_request.SetDeleteRequest();
  auto s = http_request.SetPostEmptyBody();
  ASSERT_TRUE(errors::IsFailedPrecondition(s));
  EXPECT_TRUE(StringPiece(s.error_message())
                  .contains("HTTP method has been already set"));
}

TEST(HttpRequestTest, WrongSequenceOfCalls_NotInitialized) {
  FakeLibCurl libcurl("", 200);
  HttpRequest http_request(&libcurl);

  auto s = http_request.SetPostEmptyBody();
  ASSERT_TRUE(errors::IsFailedPrecondition(s));
  EXPECT_TRUE(StringPiece(s.error_message())
                  .contains("The object has not been initialized"));
}

TEST(HttpRequestTest, EscapeString) {
  FakeLibCurl libcurl("get response", 200);
  HttpRequest http_request(&libcurl);
  TF_EXPECT_OK(http_request.Init());
  const string test_string = "a/b/c";
  EXPECT_EQ("a%2Fb%2Fc", http_request.EscapeString(test_string));
}

}  // namespace
}  // namespace tensorflow