Bug 1032573 part 4 - Add AnimationTimeline::ToTimelineTime helper method; r=dbaron
[gecko.git] / netwerk / test / TestUpload.cpp
blobfbab28c634b16c5fd50411f93ac4725127e8db8f
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #include "TestCommon.h"
7 #include <algorithm>
8 #ifdef WIN32
9 #include <windows.h>
10 #endif
12 #include "nsIComponentRegistrar.h"
13 #include "nsIIOService.h"
14 #include "nsIServiceManager.h"
15 #include "nsNetUtil.h"
17 #include "nsIUploadChannel.h"
19 static NS_DEFINE_CID(kIOServiceCID, NS_IOSERVICE_CID);
21 #include "prlog.h"
22 #if defined(PR_LOGGING)
24 // set NSPR_LOG_MODULES=Test:5
26 static PRLogModuleInfo *gTestLog = nullptr;
27 #endif
28 #define LOG(args) PR_LOG(gTestLog, PR_LOG_DEBUG, args)
30 //-----------------------------------------------------------------------------
31 // InputTestConsumer
32 //-----------------------------------------------------------------------------
34 class InputTestConsumer : public nsIStreamListener
36 virtual ~InputTestConsumer();
38 public:
40 InputTestConsumer();
42 NS_DECL_ISUPPORTS
43 NS_DECL_NSIREQUESTOBSERVER
44 NS_DECL_NSISTREAMLISTENER
47 InputTestConsumer::InputTestConsumer()
51 InputTestConsumer::~InputTestConsumer()
55 NS_IMPL_ISUPPORTS(InputTestConsumer,
56 nsIStreamListener,
57 nsIRequestObserver)
59 NS_IMETHODIMP
60 InputTestConsumer::OnStartRequest(nsIRequest *request, nsISupports* context)
62 LOG(("InputTestConsumer::OnStartRequest\n"));
63 return NS_OK;
66 NS_IMETHODIMP
67 InputTestConsumer::OnDataAvailable(nsIRequest *request,
68 nsISupports* context,
69 nsIInputStream *aIStream,
70 uint64_t aSourceOffset,
71 uint32_t aLength)
73 char buf[1025];
74 uint32_t amt, size;
75 nsresult rv;
77 while (aLength) {
78 size = std::min<uint32_t>(aLength, sizeof(buf));
79 rv = aIStream->Read(buf, size, &amt);
80 if (NS_FAILED(rv)) {
81 NS_ASSERTION((NS_BASE_STREAM_WOULD_BLOCK != rv),
82 "The stream should never block.");
83 return rv;
85 aLength -= amt;
87 return NS_OK;
91 NS_IMETHODIMP
92 InputTestConsumer::OnStopRequest(nsIRequest *request, nsISupports* context,
93 nsresult aStatus)
95 LOG(("InputTestConsumer::OnStopRequest [status=%x]\n", aStatus));
96 QuitPumpingEvents();
97 return NS_OK;
102 main(int argc, char* argv[])
104 if (test_common_init(&argc, &argv) != 0)
105 return -1;
107 nsresult rv;
109 if (argc < 2) {
110 printf("usage: %s <url> <file-to-upload>\n", argv[0]);
111 return -1;
113 char* uriSpec = argv[1];
114 char* fileName = argv[2];
116 #if defined(PR_LOGGING)
117 gTestLog = PR_NewLogModule("Test");
118 #endif
121 nsCOMPtr<nsIServiceManager> servMan;
122 NS_InitXPCOM2(getter_AddRefs(servMan), nullptr, nullptr);
124 // first thing to do is create ourselves a stream that
125 // is to be uploaded.
126 nsCOMPtr<nsIInputStream> uploadStream;
127 rv = NS_NewPostDataStream(getter_AddRefs(uploadStream),
128 true,
129 nsDependentCString(fileName)); // XXX UTF-8
130 if (NS_FAILED(rv)) return -1;
132 nsCOMPtr<nsIIOService> ioService(do_GetService(kIOServiceCID, &rv));
134 // create our url.
135 nsCOMPtr<nsIURI> uri;
136 rv = NS_NewURI(getter_AddRefs(uri), uriSpec);
137 if (NS_FAILED(rv)) return -1;
139 nsCOMPtr<nsIChannel> channel;
140 rv = ioService->NewChannelFromURI(uri, getter_AddRefs(channel));
141 if (NS_FAILED(rv)) return -1;
143 // QI and set the upload stream
144 nsCOMPtr<nsIUploadChannel> uploadChannel(do_QueryInterface(channel));
145 uploadChannel->SetUploadStream(uploadStream, EmptyCString(), -1);
147 // create a dummy listener
148 InputTestConsumer* listener;
150 listener = new InputTestConsumer;
151 if (!listener) {
152 NS_ERROR("Failed to create a new stream listener!");
153 return -1;
155 NS_ADDREF(listener);
157 channel->AsyncOpen(listener, nullptr);
159 PumpEvents();
160 } // this scopes the nsCOMPtrs
161 // no nsCOMPtrs are allowed to be alive when you call NS_ShutdownXPCOM
162 rv = NS_ShutdownXPCOM(nullptr);
163 NS_ASSERTION(NS_SUCCEEDED(rv), "NS_ShutdownXPCOM failed");
165 return 0;