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

fuchsia_web / common / test / test_devtools_list_fetcher.cc [blame]

// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "fuchsia_web/common/test/test_devtools_list_fetcher.h"

#include <memory>
#include <string>

#include "base/json/json_reader.h"
#include "base/strings/stringprintf.h"
#include "net/http/http_status_code.h"
#include "net/traffic_annotation/network_traffic_annotation_test_helper.h"
#include "net/url_request/url_request_context.h"
#include "net/url_request/url_request_context_builder.h"
#include "net/url_request/url_request_test_util.h"

base::Value::List GetDevToolsListFromPort(uint16_t port) {
  GURL url(base::StringPrintf("http://127.0.0.1:%d/json/list", port));
  auto request_context = net::CreateTestURLRequestContextBuilder()->Build();
  net::TestDelegate delegate;

  std::unique_ptr<net::URLRequest> request(request_context->CreateRequest(
      url, net::DEFAULT_PRIORITY, &delegate, TRAFFIC_ANNOTATION_FOR_TESTS));
  request->Start();
  delegate.RunUntilComplete();

  if (delegate.request_status() < 0)
    return base::Value::List();

  if (request->response_headers()->response_code() != net::HTTP_OK)
    return base::Value::List();

  const std::string& result = delegate.data_received();
  if (result.empty())
    return base::Value::List();

  return base::JSONReader::Read(result)
      .value_or(base::Value())
      .GetList()
      .Clone();
}