PhpUnit unable to access external url - unit-testing

On my local windows PC I am running XAMPP and it is serving a testpage on it (e.g. http://localhost/testsite/testpage.html)
Now on the same machine I have an instance of laravel 5.2 running and I have one named route in it called testroute.
I write a phpunit test cases
public function testBasicExample1() {
$this->visit('testroute')->see('Something'); //Passes
}
public function testBasicExample2() {
$this->visit('http://www.google.com')->see('Google'); //Passes
}
public function testBasicExample3() {
$this->visit('http://localhost/testsite/testpage.html')->see('Something Else');
//Fails as it is unable to reach the desired page (Received status code [404])
}
in TestCase.php
$baseUrl = 'http://localhost:8000';
and in .env APP_URL=http://localhost:8000
Is it know that localhost sites cannot be accessed in phpunit?
Update:
I figured out even http://www.google.com is not working, it is redirecting to the laravel's welcome route. (test passed as there was text 'Google' in that page as well). Basically it was trying to assess http://localhost:8000/www.google.com and that redirects to welcome page.
I am not sure how in laravel's phpunit I can access external url.

I banged my head against a wall for a long time with this. I don't believe it is possible / functional to test external sites with the Laravel click() or visit() methods. If it is, I'm not seeing it.
Though my need was to just check all my links, perhaps this hack may be helpful to you. I went back to basic php to assert the sites returned properly.
$sites = \App\Website::pluck('website');
foreach($sites as $site) {
$file_headers = #get_headers($site);
if (strpos($file_headers[0], '404 Not Found') || $file_headers[0] == null) {
$exists = false;
echo " Failed on: ".$site." ";
}
else {
$exists = true;
}
$this->assertTrue($exists);
}
It doesn't quite get you all the way to what you want (seeing something), but for me it was good enough to be able to see the link was live and successful.
Testing is slow as it is going out to x # of sites.
HTH

Related

Ember - Change current route from browser console

I am not at all Ember developer, but I would like to change current route from browser console. Is it possible at all to access correctly Ember, e.g. Ember.Router.prototype.transitionTo('/feed')?
Version of the website is 3.16.9
After a lot of research, I have found out possible solutions that you can use. I was trying to achieve it on Linkedin website via Chrome Extension
function runEmbedded(path) {
const namespaces = window.Ember.Namespace.NAMESPACES;
let application;
namespaces.forEach(function (namespace) {
if (namespace instanceof window.Ember.Application) {
application = namespace;
return false;
}
});
application.__container__.lookup('router:main').transitionTo(path);
}
const payload = '/some/new-path'
script.text = `(${runEmbedded.toString()})('${payload}');`;
document.documentElement.appendChild(script);
Second solution/hack:
Another possible hack to use, when the website is not listening to pushState/replaceState actions from History API is to push state 2 times and then go back. Please remember that's only a hack.
history.pushState({}, '', msg.payload);
history.pushState({}, '', msg.payload);
history.back();

“The page has expired due to inactivity” appears when using a services methods - Laravel 5.5

According to other asked questions like this one, I did many doings to prevent this request expired message but there is no solution for my issue.
In the long run I recognized that the message appears when I call a service method inside a controller which run on form action!
Here is my codes samples with some descriptions:
My route:
Route::post('Material/{id}', 'MaterialController#updateMaterial')->name('updateMaterial');
Material Controller Constructor:
public function __construct(CustomService $srv)
{
$this->middleware('admin')->only(['updateMaterial']);
$this->srv= $srv;
}
srv is a protected attribute in MaterialController class.
updateMaterial Method:
public function updateMaterial($id,Request $request)
{
$this->validate($request, [...]);
$material = $this->srv->updateMaterial($request, $id);
if ($material)
return view('panel._materials.edit-material')
->with('material', $material)
->with('success', 1);
}
I also have a provider for CustomService with name CustomServiceProvider and here is the register method of the provider:
public function register()
{
$this->app->bind(CustomService::class,function($app){
return new CustomService();
});
}
and I registered it as a provider in config/app.php.
So when I return something before calling service updateMaterial method, it's OK. but when the method runs, the issue appears!
I don'n have any idea about!
Update:
And here is updateMaterial of CustomService:
public function updateMaterial($request, $id)
{
$material = Material::find($id);
if (!$material)
return false;
if ($request->has('unit'))
$material->unit = $request['unit'];
if ($request->has('price'))
$material->price = $request['price'];
if ($request->has('type'))
$material->type = $request['type'];
if ($request->has('is_active'))
$material->is_active = $request['is_active'];
$material->updated_at = Carbon::now();
$material->save();
return $material;
}
I also create a new project with Laravel 5.5.0 and without adding any complexity I just added a post route and call it in form action, but nothing changed!
This is just an issue for Windows users on Local Environment. I suffered a lot with this also when on Windows. Once you deploy to your production server, you won't have any issue at all.
It's important to note that this is not an issue with Laravel 5.5 version only. I first saw this issue in version 5.2.
I think a good fix for this would maybe be using something like Homestead or Vessel from Fideloper. Honestly I only suffered this problem when using Windows.

cgi.SERVER_NAME reverts origin

I have two versions of a site, one for spanish, one for english. The spanish subdomain is set via IIS and a C Name (network admin told me, I'm not sure how or what that means), it's not a separate subdomain.
es.website.com
en.website.com
Now, when I use CGI.SERVER_NAME on my development server, everything works nicely. However, in production, when I'm on es.website.com, despite my Application.cfc settings, it thinks the origin is en.website.com, which throws off my <cfheader name="Access-Control-Allow-Origin" value="#application.site#">.
Here is how I differentiate the domains and sites to determine which content must be in spanish:
application.subdomain = ListFirst(cgi.SERVER_NAME, ".");
if (application.test) {
if (application.subdomain == "en") {
application.site = "http://en.dev.website.com/";
} else {
application.site = "http://es.dev.website.com/";
}
} else {
if (application.subdomain == "en") {
application.site = "http://en.website.com/";
} else {
application.site = "http://es.website.com/";
}
}
I cannot figure out why when on other pages, application.sites is clearly es.website.com, yet on some pages, the cgi.server_name reverts to en.website.com. Any insight?
If you are storing it in an application scoped variable then users can change the variable mid request. You don't see this on your dev server because you don't have any concurrent users.
Assume you have a request to en.website.com then 1 millisecond later a request to es.website.com both requests will share the same application scope, the second request will change the value of application.site to the ES version.
A better solution would be to use a request scoped variable for this since the value differs by request.
Another less elegant solution would be to make sure each site has a different application name, for example:
this.name = LCase(cgi.server_name) & "_website";
That would cause each domain to have its own application scope, which depending on how your web server is setup could lead to a denial of service condition (if you allow any domain to hit the application).

Why am I getting "The ExternalInterface is not available in this container" in a Flex Mobile Project?

I am using StageWebView to show a local HTML page (using file://). I want to call a function in my flex mobile project to be called from a JS function. Using ExternalInterface, I have
in Flex -
ExternalInterface.addCallback("myFunction",myFunc);
in JS -
function thisMovie(movieName) {
if (navigator.appName.indexOf("Microsoft") != -1) {
return window[movieName];
} else {
return document[movieName];
}
}
function showAlert()
{
alert("Going to call AS function");
thisMovie("ShowLocalHTML").myFunction("Hello");
return false;
}
I am getting "Error: Error #2067: The ExternalInterface is not available in this container. ExternalInterface requires Internet Explorer ActiveX, Firefox, Mozilla 1.7.5 and greater, or other browsers that support NPRuntime." when trying to run the application.
And my project targets Android platform. And I have Mozilla, Chrome installed on my desktop - although I am not user if that is relevant to the problem.
Please help in resolving this issue.
Since I am new to Flex and AS it took me a while to understand the problem. See http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/external/ExternalInterface.html and http://sean.voisen.org/blog/2010/10/making-the-most-of-stagewebview/. What it means is External Interface is not available for use with StageWebView in AIR for android.
True, but you still can communicate js <> air if you are using an ANE solution. Check here https://github.com/myflashlab/webView-ANE I'm the coder of this ANE by the way! :)

How can I use IIS URL Rewrite to prevent people from linking or leeching images on a website?

I came across the features of IIS and it notes that I can prevent inline linking or leeching. How can I implement such a rule?
You will need to use URL rewrite to point to a resource within a web application that will be able to identify the refferer before displaying the resource to the client which called the application.
IE, rewrite the following
/images/myPhoto.JPG
to
/getResource.PHP?resource=%2Fimages%2FmyPhoto.JPG
Then inside getResource.PHP
forgive me if this isn't quite right, my php is a little sketchy, but you get the idea
header('Content-Type: image/jpeg');
if(isset($_SERVER['HTTP_REFERER'])) {
if( /*test that it fits your criteria*/ true ) {
$imagepath= $_GET['resource'];
} else {
$imagepath= "/images/stopLeaching.JPG";
}
} else {
$imagepath= $_GET['resource'];
}
$image=imagecreatefromjpeg($imagepath);
imagejpeg($image);